2

My part of java code is below.

while (status == DOWNLOADING) {
    /* Size buffer according to how much of the
       file is left to download. */
            byte buffer[];
            if (size - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[size - downloaded];
            }

            // Read from server into buffer.
            int read = stream.read(buffer);
            if (read == -1){
                System.out.println("File was downloaded");
                break;
            }

            // Write buffer to file.
            file.write(buffer, 0, read);
            downloaded += read;

        }

  /* Change status to complete if this point was
     reached because downloading has finished. */
        if (status == DOWNLOADING) {
            status = COMPLETE;

        }

I want to show the progress of the downloading file as percentage by updating the progress line in console. Please help. Thanks.

ntf
  • 1,323
  • 2
  • 12
  • 17
  • you could try using a `\r` at the end of a `System.out.print()`. This will move the cursor to the beginning of the same line. – Alex Gittemeier Apr 05 '13 at 20:48
  • @SotiriosDelimanolis What do you mean?I couldn't understand.For example, I want to show %5 and %6 and finally %100, but each time the previous percantage will be deleted and new one will be shown. – ntf Apr 05 '13 at 20:49
  • 1
    http://stackoverflow.com/questions/1001290/console-based-progress-in-java – jalopaba Apr 05 '13 at 20:54

4 Answers4

3

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.

condorcraft110
  • 195
  • 1
  • 2
  • 8
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
3

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
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;
    }
}
Elphara77
  • 11
  • 2
  • Welcome to Stack Overflow! While you may have solved this user's problem, code-only answers are not very helpful to users who come to this question in the future. Please edit your answer to explain why your code solves the original problem. – Joe C Mar 18 '17 at 22:22
-1

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

Alex Turbin
  • 2,554
  • 2
  • 22
  • 35