最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Java手搓一個控制臺進度條打印工具

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

假期在家閑來無事,突發(fā)奇想能不能自己用Java寫一個控制臺進度條打印工具,然后馬上開干。

1. 效果圖

圖1 默認(rèn)打印

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

圖3 動態(tài)打印效果展示

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("任務(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();
    }

    /**
     * 打印進度條
     *
     * @param total 任務(wù)總數(shù)
     * @param completed 已完成任務(wù)數(shù)量
     * @param length 進度條的長度,單位為字符
     * @param character 填充進度進度條字符
     * @param state 進度條的狀態(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");
        }
    }

    /**
     * 打印進度條
     *
     * @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);
    }

    /**
     * 創(chuàng)建進度條構(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 "";
        }
    }

    /**
     * 進度條狀態(tài)枚舉類
     */
    public static enum State {
        // 正?;蚰J(rèn)
        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;
        }
    }

    /**
     * 進度條構(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手搓一個控制臺進度條打印工具的文章就介紹到這了,更多相關(guān)Java進度條打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

黄龙县| 绥江县| 古田县| 沙洋县| 马山县| 札达县| 抚顺县| 珠海市| 宁陕县| 崇州市| 丹棱县| 嵩明县| 商南县| 民勤县| 永康市| 伊金霍洛旗| 四会市| 昌吉市| 皋兰县| 永顺县| 扶绥县| 垦利县| 上栗县| 易门县| 盱眙县| 陕西省| 铜梁县| 福清市| 东阳市| 渝北区| 十堰市| 安图县| 广南县| 宁武县| 建始县| 龙胜| 洪江市| 晋中市| 新田县| 南木林县| 建阳市|