Command line progress bar in Java

前端 未结 15 1999
[愿得一人]
[愿得一人] 2020-11-28 01:04

I have a Java program running in command line mode. I would like to display a progress bar, showing the percentage of job done. The same kind of progress bar you would see u

15条回答
  •  -上瘾入骨i
    2020-11-28 01:58

    Here is a modified version of the above:

    private static boolean loading = true;
    private static synchronized void loading(String msg) throws IOException, InterruptedException {
        System.out.println(msg);
        Thread th = new Thread() {
            @Override
            public void run() {
                try {
                    System.out.write("\r|".getBytes());
                    while(loading) {
                        System.out.write("-".getBytes());
                        Thread.sleep(500);
                    }
                    System.out.write("| Done \r\n".getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        th.start();
    }
    

    ... and in main:

    loading("Calculating ...");
    

提交回复
热议问题