使用Java手搓一個(gè)控制臺(tái)進(jìn)度條打印工具
假期在家閑來(lái)無(wú)事,突發(fā)奇想能不能自己用Java寫(xiě)一個(gè)控制臺(tái)進(jìn)度條打印工具,然后馬上開(kāi)干。
1. 效果圖

圖1 默認(rèn)打印

圖2 帶狀態(tài)和任務(wù)名稱(chēng)打印

圖3 動(dòng)態(tài)打印效果展示
2. 代碼
/**
* 控制臺(tái)進(jìn)度條打印工具
*/
public class ProgressBarPrintingUtils {
/**
* 使用示例
*/
public static void main(String[] args) throws InterruptedException {
// 1.進(jìn)度條打?。ㄖ苯觽鲄ⅲ?
System.out.println("進(jìn)度條打印1");
printProgressBar(100, 50, 30, '=', null, null, null);
System.out.println();
// 2.進(jìn)度條打印(通過(guò)進(jìn)度條對(duì)象傳參)
System.out.println("\n進(jìn)度條打印2");
printProgressBar(new ProgressBarBuilder()
.total(100)
.completed(80)
.length(30)
.character('#')
.state(State.ERROR)
.name("任務(wù)一")
.description("")
.build());
System.out.println();
// 3.動(dòng)態(tài)效果演示
System.out.println("\n動(dòng)態(tài)效果演示");
Random r = new Random();
ProgressBar progressBar = createProgressBarBuilder()
.total(100)
.completed(0)
.length(30)
.character('█')
.name("解析文件內(nèi)容")
.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();
}
/**
* 打印進(jìn)度條
*
* @param total 任務(wù)總數(shù)
* @param completed 已完成任務(wù)數(shù)量
* @param length 進(jìn)度條的長(zhǎng)度,單位為字符
* @param character 填充進(jìn)度進(jìn)度條字符
* @param state 進(jìn)度條的狀態(tài),用于在控制臺(tái)顯示不同的文字顏色
*
*/
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");
}
}
/**
* 打印進(jìn)度條
*
* @param progressBar 進(jìn)度條配置信息
*/
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);
}
/**
* 創(chuàng)建進(jìn)度條構(gòu)造器
*/
public static ProgressBarBuilder createProgressBarBuilder() {
return new ProgressBarBuilder();
}
/**
* 通過(guò)狀態(tài)枚舉獲取顏色開(kāi)始標(biāo)簽
*
* @param state 狀態(tài)
*/
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 "";
}
}
/**
* 進(jìn)度條狀態(tài)枚舉類(lèi)
*/
public static enum State {
// 正?;蚰J(rèn)
NORMAL,
// 警告
WARNING,
// 錯(cuò)誤
ERROR,
// 成功
SUCCESS
}
/**
* 進(jìn)度條類(lèi)
*/
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;
}
}
/**
* 進(jìn)度條構(gòu)造類(lèi)
*/
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);
}
}
}
到此這篇關(guān)于使用Java手搓一個(gè)控制臺(tái)進(jìn)度條打印工具的文章就介紹到這了,更多相關(guān)Java進(jìn)度條打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
深入淺析Mybatis與Hibernate的區(qū)別與用途
這篇文章主要介紹了Mybatis與Hibernate的區(qū)別與用途的相關(guān)資料,需要的朋友可以參考下2017-10-10
SpringMVC中的SimpleUrlHandlerMapping用法詳解
這篇文章主要介紹了SpringMVC中的SimpleUrlHandlerMapping用法詳解,SimpleUrlHandlerMapping是Spring MVC中適用性最強(qiáng)的Handler Mapping類(lèi),允許明確指定URL模式和Handler的映射關(guān)系,有兩種方式聲明SimpleUrlHandlerMapping,需要的朋友可以參考下2023-10-10
Java常見(jiàn)報(bào)錯(cuò)類(lèi)型及解決方案詳細(xì)解析(從異常處理到錯(cuò)誤排查)
這篇文章主要介紹了Java常見(jiàn)報(bào)錯(cuò)類(lèi)型及解決方案的相關(guān)資料,文中結(jié)合具體案例提供針對(duì)性解決方案,幫助開(kāi)發(fā)者快速定位并修復(fù)問(wèn)題,需要的朋友可以參考下2025-05-05
基于SpringBoot實(shí)現(xiàn)HTML轉(zhuǎn)PDF功能的常用方式
你是否因?yàn)镠tml轉(zhuǎn)PDF發(fā)愁?又是否因?yàn)橹形膩y碼而心煩意亂?又是否因?yàn)楦袷綄?dǎo)致PDF排版錯(cuò)亂而苦惱?所以本文小編給大家介紹了基于SpringBoot實(shí)現(xiàn)HTML轉(zhuǎn)PDF功能的常用方式,需要的朋友可以參考下2025-10-10
使用ServletInputStream在攔截器或過(guò)濾器中應(yīng)用后重寫(xiě)
這篇文章主要介紹了使用ServletInputStream在攔截器或過(guò)濾器中應(yīng)用后重寫(xiě),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
ZooKeeper官方文檔之Java客戶(hù)端開(kāi)發(fā)案例翻譯
網(wǎng)上有很多ZooKeeper的java客戶(hù)端例子,我也看過(guò)很多,不過(guò)大部分寫(xiě)的都不好,有各種問(wèn)題。兜兜轉(zhuǎn)轉(zhuǎn)還是覺(jué)得官方給的例子最為經(jīng)典,在學(xué)習(xí)之余翻譯下來(lái),供朋友們參考2022-01-01
java對(duì)象數(shù)組實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java對(duì)象數(shù)組實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06

