假期在家闲来无事,突发奇想能不能自己用java写一个控制台进度条打印工具,然后马上开干。
1. 效果图

图1 默认打印

图2 带状态和任务名称打印

图3 动态打印效果展示
2. 代码
/**
* 控制台进度条打印工具
*/
public class progressbarprintingutils {
/**
* 使用示例
*/
public static void main(string[] args) throws interruptedexception {
// 1.进度条打印(直接传参)
system.out.println("进度条打印1");
printprogressbar(100, 50, 30, '=', null, null, null);
system.out.println();
// 2.进度条打印(通过进度条对象传参)
system.out.println("\n进度条打印2");
printprogressbar(new progressbarbuilder()
.total(100)
.completed(80)
.length(30)
.character('#')
.state(state.error)
.name("任务一")
.description("")
.build());
system.out.println();
// 3.动态效果演示
system.out.println("\n动态效果演示");
random r = new random();
progressbar progressbar = createprogressbarbuilder()
.total(100)
.completed(0)
.length(30)
.character('█')
.name("解析文件内容")
.build();
while (true) {
printprogressbar(progressbar);
if (progressbar.completed >= progressbar.total) {
break;
}
thread.sleep(500);
progressbar.completed += r.nextint(10);
if (progressbar.getcompleted() >= progressbar.gettotal()) {
progressbar.setcompleted(progressbar.gettotal());
progressbar.setstate(state.success);
progressbar.setdescription("解析完成");
}
}
system.out.println();
}
/**
* 打印进度条
*
* @param total 任务总数
* @param completed 已完成任务数量
* @param length 进度条的长度,单位为字符
* @param character 填充进度进度条字符
* @param state 进度条的状态,用于在控制台显示不同的文字颜色
*
*/
public static void printprogressbar(int total, int completed, int length, char character,
state state, string name, string description) {
system.out.print("\r");
system.out.print(getcolorstarttagbystate(state));
if (name != null) {
system.out.print(name);
system.out.print(" ");
}
system.out.print("[");
int ratio = completed >= total ? length : (int) math.floor(completed / (double)total * length);
for (int i = 1; i <= ratio; i++) {
system.out.print(character);
}
for (int i = 1; i <= length - ratio; i++) {
system.out.print(" ");
}
system.out.print("] ");
system.out.print(completed);
system.out.print("/");
system.out.print(total);
system.out.print(" ");
if (description != null) {
system.out.print(description);
}
if (state != null) {
system.out.print("\033[0m");
}
}
/**
* 打印进度条
*
* @param progressbar 进度条配置信息
*/
public static void printprogressbar(progressbar progressbar) {
if (progressbar == null) {
return;
}
printprogressbar(progressbar.total, progressbar.completed, progressbar.length, progressbar.character,
progressbar.state, progressbar.name, progressbar.description);
}
/**
* 创建进度条构造器
*/
public static progressbarbuilder createprogressbarbuilder() {
return new progressbarbuilder();
}
/**
* 通过状态枚举获取颜色开始标签
*
* @param state 状态
*/
private static string getcolorstarttagbystate(state state) {
if (state == null) {
return "";
}
switch (state) {
case error:
return "\033[31m";
case success:
return "\033[32m";
case warning:
return "\033[33m";
default:
return "";
}
}
/**
* 进度条状态枚举类
*/
public static enum state {
// 正常或默认
normal,
// 警告
warning,
// 错误
error,
// 成功
success
}
/**
* 进度条类
*/
public static class progressbar {
private int total;
private int completed;
private int length;
private char character;
private state state;
private string name;
private string description;
private progressbar(progressbarbuilder builder) {
this.total = builder.total;
this.completed = builder.completed;
this.length = builder.length;
this.character = builder.character;
this.state = builder.state;
this.name = builder.name;
this.description = builder.description;
}
public int gettotal() {
return total;
}
public void settotal(int total) {
this.total = total;
}
public int getcompleted() {
return completed;
}
public void setcompleted(int completed) {
this.completed = completed;
}
public int getlength() {
return length;
}
public void setlength(int length) {
this.length = length;
}
public char getcharacter() {
return character;
}
public void setcharacter(char character) {
this.character = character;
}
public state getstate() {
return state;
}
public void setstate(state state) {
if (state == null) {
this.state = state.normal;
return;
}
this.state = state;
}
public string getname() {
return name;
}
public void setname(string name) {
if (name == null) {
this.name = "";
}
this.name = name;
}
public string getdescription() {
return description;
}
public void setdescription(string description) {
if (description == null) {
this.description = "";
}
this.description = description;
}
}
/**
* 进度条构造类
*/
public static class progressbarbuilder {
private int total;
private int completed;
private int length;
private char character;
private state state;
private string name;
private string description;
public progressbarbuilder() {
this.total = 100;
this.completed = 0;
this.length = 20;
this.character = '=';
this.state = state.normal;
this.name = "";
this.description = "";
}
public progressbarbuilder total(int total) {
this.total = total;
return this;
}
public progressbarbuilder completed(int completed) {
this.completed = completed;
return this;
}
public progressbarbuilder length(int length) {
this.length = length;
return this;
}
public progressbarbuilder character(char character) {
this.character = character;
return this;
}
public progressbarbuilder state(state state) {
this.state = state;
return this;
}
public progressbarbuilder name(string name) {
if (name == null) {
name = "";
}
this.name = name;
return this;
}
public progressbarbuilder description(string description) {
if (description == null) {
description = "";
}
this.description = description;
return this;
}
public progressbar build() {
return new progressbar(this);
}
}
}
到此这篇关于使用java手搓一个控制台进度条打印工具的文章就介绍到这了,更多相关java进度条打印内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论