How to show percentage progress of a downloading file in java console(without UI)?

后端 未结 4 518
不知归路
不知归路 2020-12-12 00:57

My part of java code is below.

while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
       file is left to download. */
                   


        
相关标签:
4条回答
  • First, you cannot emulate real progress bar on pure console.

    @Jeffrey Blattman provided a solution for simple outputting to console as well as library that provides advanced working with console

    if you are more interested in building UI in console, you may try Lanterna (http://code.google.com/p/lanterna/) - this is really good stuff especially if you wish to develop software for old-school departments like libraries or governmental accountants

    0 讨论(0)
  • 2020-12-12 01:24

    This is a simple "progress bar" concept.

    Basically it uses the \b character to backspace over characters previously printed to the console before printing out the new progress bar...

    public class TextProgress {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            System.out.println("");
            printProgress(0);
            try {
                for (int index = 0; index < 101; index++) {
                    printProgress(index);
                    Thread.sleep(125);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(TextProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    
        public static void printProgress(int index) {
    
            int dotCount = (int) (index / 10f);
            StringBuilder sb = new StringBuilder("[");
            for (int count = 0; count < dotCount; count++) {
                sb.append(".");
            }
            for (int count = dotCount; count < 10; count++) {
                sb.append(" ");
            }
            sb.append("]");
            for (int count = 0; count < sb.length(); count++) {
                System.out.print("\b");
            }
            System.out.print(sb.toString());
    
        }
    }
    

    It won't work from within most IDE's, as Java's text components don't support the \b character, you must run from the terminal/console

    0 讨论(0)
  • 2020-12-12 01:29

    If you are satisfied with output like

    Downloading file ... 10% ... 20% ... 38% ...

    that keeps appending to the line, you can just print instead of println. If you want to do something limited like just update a few characters in place, you can print the backspace character to erase then re-print the percent, for example:

    System.out.print("index at: X");
    int lastSize = 1;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < lastSize; j++) {
        System.out.print("\b");
      }
      String is = String.toString(i);
      System.out.print(is);
      lastSize = is.length();
    }
    

    Note how the code tracks how many characters were printed, so it knows how many backspaces to print to erase the appended text.

    Something more complex than that, you need some sort of console or terminal control SDK. ncurses is the Unix standard, and it looks like there's a Java port:

    http://sourceforge.net/projects/javacurses/

    I have never used this one so I can't vouch for it. If it's not right, a quick Google showed many alternatives.

    0 讨论(0)
  • 2020-12-12 01:31
    public class ConsoleProgressCursor {
    
        private static String CURSOR_STRING = "0%.......10%.......20%.......30%.......40%.......50%.......60%.......70%.......80%.......90%.....100%";
    
        private double max = 0.;
        private double cursor = 1.;
        private double lastCursor = 0.;
    
        public static void main(String[] args) throws InterruptedException {
            final int max = 100;
            ConsoleProgressCursor progress = new ConsoleProgressCursor("Progress : ", max);
            for (int i = 0; i < max; i++) {
                Thread.sleep(10L);
                progress.nextProgress();
            }
            progress.endProgress();
        }
    
        public ConsoleProgressCursor(String title, double maxCounts) {
            max = maxCounts;
            System.out.print(title);
        }
    
        public void nextProgress() {
            cursor += 100. / max;
            printCursor();
        }
    
        public void nextProgress(double progress) {
            cursor += progress * 100. / max;
            printCursor();
        }
    
        public void endProgress() {
            System.out.println(CURSOR_STRING.substring((int) lastCursor, 101));
        }
    
        private void printCursor() {
            final int intCursor = (int) Math.floor(cursor);
            System.out.print(CURSOR_STRING.substring((int) lastCursor, intCursor));
            lastCursor = cursor;
        }
    }
    
    0 讨论(0)
提交回复
热议问题