使用Java手搓一個控制臺進(jìn)度條打印工具
假期在家閑來無事,突發(fā)奇想能不能自己用Java寫一個控制臺進(jìn)度條打印工具,然后馬上開干。
1. 效果圖
圖1 默認(rèn)打印
圖2 帶狀態(tài)和任務(wù)名稱打印
圖3 動態(tài)打印效果展示
2. 代碼
/** * 控制臺進(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)度條打?。ㄍㄟ^進(jìn)度條對象傳參) 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.動態(tài)效果演示 System.out.println("\n動態(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)度條的長度,單位為字符 * @param character 填充進(jìn)度進(jìn)度條字符 * @param state 進(jìn)度條的狀態(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(); } /** * 通過狀態(tà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, // 錯誤 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手搓一個控制臺進(jìn)度條打印工具的文章就介紹到這了,更多相關(guān)Java進(jìn)度條打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RestTemplate接口調(diào)用神器常見用法匯總
這篇文章主要介紹了RestTemplate接口調(diào)用神器常見用法匯總,通過案例代碼詳細(xì)介紹RestTemplate接口調(diào)用神器常見用法,需要的朋友可以參考下2022-07-07關(guān)于spring依賴注入的方式以及優(yōu)缺點
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點,依賴注入,是IOC的一個方面,是個通常的概念,它有多種解釋,這概念是說你不用創(chuàng)建對象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07idea maven 構(gòu)建本地jar包及pom文件的過程
這篇文章主要介紹了idea maven 構(gòu)建本地jar包及pom文件的過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11java中SynchronizedList和Vector的區(qū)別詳解
這篇文章主要介紹了java中SynchronizedList和Vector的區(qū)別詳解,Vector是java.util包中的一個類。 SynchronizedList是java.util.Collections中的一個靜態(tài)內(nèi)部類。,需要的朋友可以參考下2019-06-06設(shè)計模式之責(zé)任鏈模式_動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了設(shè)計模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08使用Java visualVM監(jiān)控遠(yuǎn)程JVM的流程分析
我們經(jīng)常需要對我們的開發(fā)的軟件做各種測試, 軟件對系統(tǒng)資源的使用情況更是不可少,JDK1.6開始自帶的VisualVM就是不錯的監(jiān)控工具,本文給大家分享使用Java visualVM監(jiān)控遠(yuǎn)程JVM的問題,感興趣的朋友跟隨小編一起看看吧2021-05-05Spring在@ConditionalOnProperty注解使用詳解
這篇文章主要介紹了Spring在@ConditionalOnProperty注解使用詳解,@ConditionalOnProperty注解是Spring Boot的條件注解,主要用法是根據(jù)配置文件中的屬性來控制某個配置類是否生效,或者控制某個Bean是否被創(chuàng)建,需要的朋友可以參考下2023-11-11