使用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ù)名稱打印

圖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)枚舉類
*/
public static enum State {
// 正?;蚰J(rèn)
NORMAL,
// 警告
WARNING,
// 錯(cuò)誤
ERROR,
// 成功
SUCCESS
}
/**
* 進(jìn)度條類
*/
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)造類
*/
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)文章
RestTemplate接口調(diào)用神器常見(jiàn)用法匯總
這篇文章主要介紹了RestTemplate接口調(diào)用神器常見(jiàn)用法匯總,通過(guò)案例代碼詳細(xì)介紹RestTemplate接口調(diào)用神器常見(jiàn)用法,需要的朋友可以參考下2022-07-07
關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn)
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點(diǎn),依賴注入,是IOC的一個(gè)方面,是個(gè)通常的概念,它有多種解釋,這概念是說(shuō)你不用創(chuàng)建對(duì)象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07
idea maven 構(gòu)建本地jar包及pom文件的過(guò)程
這篇文章主要介紹了idea maven 構(gòu)建本地jar包及pom文件的過(guò)程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11
Java實(shí)現(xiàn)遞歸刪除菜單和目錄及目錄下所有文件
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)遞歸刪除菜單和刪除目錄及目錄下所有文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03
java中SynchronizedList和Vector的區(qū)別詳解
這篇文章主要介紹了java中SynchronizedList和Vector的區(qū)別詳解,Vector是java.util包中的一個(gè)類。 SynchronizedList是java.util.Collections中的一個(gè)靜態(tài)內(nèi)部類。,需要的朋友可以參考下2019-06-06
設(shè)計(jì)模式之責(zé)任鏈模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了設(shè)計(jì)模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
使用Java visualVM監(jiān)控遠(yuǎn)程JVM的流程分析
我們經(jīng)常需要對(duì)我們的開(kāi)發(fā)的軟件做各種測(cè)試, 軟件對(duì)系統(tǒng)資源的使用情況更是不可少,JDK1.6開(kāi)始自帶的VisualVM就是不錯(cuò)的監(jiān)控工具,本文給大家分享使用Java visualVM監(jiān)控遠(yuǎn)程JVM的問(wèn)題,感興趣的朋友跟隨小編一起看看吧2021-05-05
Spring在@ConditionalOnProperty注解使用詳解
這篇文章主要介紹了Spring在@ConditionalOnProperty注解使用詳解,@ConditionalOnProperty注解是Spring Boot的條件注解,主要用法是根據(jù)配置文件中的屬性來(lái)控制某個(gè)配置類是否生效,或者控制某個(gè)Bean是否被創(chuàng)建,需要的朋友可以參考下2023-11-11
MyBatis Properties及別名定義實(shí)例詳解
這篇文章主要介紹了MyBatis Properties及別名定義實(shí)例詳解,需要的朋友可以參考下2017-08-08

