Java实现控制台输出进度条

        char incomplete = '░'; // U+2591 Unicode Character 表示还没有完成的部分
        char complete = '█'; // U+2588 Unicode Character 表示已经完成的部分

        int total = 100;

        StringBuilder stringBuilder = new StringBuilder();
        Stream.generate(() -> incomplete).limit(total).forEach(stringBuilder::append);
        
        for (int i = 0; i < total; i++) {
            stringBuilder.replace(i, i + 1, String.valueOf(complete));
            String progressBar = "\r" + stringBuilder;
            String percent = " " + (i + 1) + "%";
            System.out.print(progressBar + percent);
            //模拟进度越接近结束越慢
            try {
                Thread.sleep(i * 5L);
            } catch (InterruptedException ignored) {

            }
        }

 

 

你可能感兴趣的:(Java,java,开发语言)