- 源码
package com.yang.restdemo.util;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
/**
* @description: 进度条
* @author: Yang JianXiong
* @since: 2022/11/26
*/
public class ProgressBar {
private static final int TOTAL = 100;
private static final char BACK_GROUND = '░';
private static final char FRONT_GROUND = '█';
public static void main(String[] args) throws InterruptedException {
StringBuilder stringBuilder = new StringBuilder();
Stream.generate(() -> BACK_GROUND).limit(TOTAL).forEach(stringBuilder::append);
for (int i = 0; i < TOTAL; i++) {
stringBuilder.replace(i, i + 1, String.valueOf(FRONT_GROUND));
String bar = "\r" + stringBuilder;
String percent = " " + (1 + i) + "%";
// 输出绿色进度条
System.out.print(ProgressBar.ColorEnum.GREEN.getValue() + bar + percent);
// 防止打印太快
TimeUnit.MILLISECONDS.sleep(i + 2);
}
}
/**
* 颜色枚举
*/
enum ColorEnum {
/**
* 白色
*/
WHITE("\33[0m"),
/**
* 红色
*/
RED("\33[1m\33[31m"),
/**
* 绿色
*/
GREEN("\33[1m\33[32m"),
/**
* 黄色
*/
YELLOW("\33[1m\33[33m"),
/**
* 蓝色
*/
BLUE("\33[1m\33[34m"),
/**
* 粉色
*/
PINK("\33[1m\33[35m"),
/**
* 青色
*/
CYAN("\33[1m\33[36m");
/**
* 颜色值
*/
private String value;
ColorEnum(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
- 输出效果
██████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 58%
标签:Java,String,进度条,33,value,static,控制台,1m
From: https://www.cnblogs.com/JaxYoun/p/16928445.html