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 using wget under unix. Is this possible?
I have implemented this sort of thing before. Its not so much about java, but what characters to send to the console.
The key is the difference between
n goes to the start of a new line. But
r is just carriage return – it goes back to the start of the same line.
So the thing to do is to print your progress bar, for example, by printing the string
On the next tick of the progress bar, overwrite the same line with a longer bar. (because we are using r, we stay on the same line) For example:
What you have to remember to do, is when done, if you then just print
You may still have some garbage from the progress bar on the line. So after you are done with the progress bar, be sure to print enough whitespace to remove it from the line. Such as:
Hope that helps.