亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Java手搓一個控制臺進(jìn)度條打印工具

 更新時間:2025年04月07日 10:51:10   作者:同志32713  
這篇文章主要為大家詳細(xì)介紹了如何使用Java手搓一個控制臺進(jìn)度條打印工具,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

假期在家閑來無事,突發(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)用神器常見用法匯總

    這篇文章主要介紹了RestTemplate接口調(diào)用神器常見用法匯總,通過案例代碼詳細(xì)介紹RestTemplate接口調(diào)用神器常見用法,需要的朋友可以參考下
    2022-07-07
  • 關(guān)于spring依賴注入的方式以及優(yōu)缺點

    關(guān)于spring依賴注入的方式以及優(yōu)缺點

    這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點,依賴注入,是IOC的一個方面,是個通常的概念,它有多種解釋,這概念是說你不用創(chuàng)建對象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下
    2023-07-07
  • idea maven 構(gòu)建本地jar包及pom文件的過程

    idea maven 構(gòu)建本地jar包及pom文件的過程

    這篇文章主要介紹了idea maven 構(gòu)建本地jar包及pom文件的過程,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2023-11-11
  • Java實現(xiàn)遞歸刪除菜單和目錄及目錄下所有文件

    Java實現(xiàn)遞歸刪除菜單和目錄及目錄下所有文件

    這篇文章主要為大家詳細(xì)介紹了Java如何實現(xiàn)遞歸刪除菜單和刪除目錄及目錄下所有文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2025-03-03
  • java中SynchronizedList和Vector的區(qū)別詳解

    java中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é)院整理

    設(shè)計模式之責(zé)任鏈模式_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了設(shè)計模式之責(zé)任鏈模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 使用Java visualVM監(jiān)控遠(yuǎn)程JVM的流程分析

    使用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-05
  • Spring在@ConditionalOnProperty注解使用詳解

    Spring在@ConditionalOnProperty注解使用詳解

    這篇文章主要介紹了Spring在@ConditionalOnProperty注解使用詳解,@ConditionalOnProperty注解是Spring Boot的條件注解,主要用法是根據(jù)配置文件中的屬性來控制某個配置類是否生效,或者控制某個Bean是否被創(chuàng)建,需要的朋友可以參考下
    2023-11-11
  • SpringBoot中獲取微信用戶信息的方法

    SpringBoot中獲取微信用戶信息的方法

    這篇文章主要介紹了SpringBoot中獲取微信用戶信息的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • MyBatis  Properties及別名定義實例詳解

    MyBatis Properties及別名定義實例詳解

    這篇文章主要介紹了MyBatis Properties及別名定義實例詳解,需要的朋友可以參考下
    2017-08-08

最新評論