This question already has an answer here:
- Command line progress bar in Java 12 answers
Is there are easy way to implement a rolling percentage for a process in Java, to be displayed in the console? I have a percentage data type (double) I generated during a particular process, but can I force it to the console window and have it refresh, instead of just printing a new line for each new update to the percentage? I was thinking about pushing a cls and updating, because I'm working in a Windows environment, but I was hoping Java had some sort of built-in capability. All suggestions welcomed! Thanks!
Clear the console by running the os specific command and then print the new percentage
I'm quite sure there is no way to change anything that the console has already printed because Java considers the console (standard out) to be a PrintStream.
You can print a carriage return
\r
to put the cursor back to the beginning of line.Example:
Don't know about anything built in to java itself, but you can use terminal control codes to do things like reposition the cursor. Some details here: http://www.termsys.demon.co.uk/vtansi.htm
I use following code:
The result:
Late for the party, but here's an answer:
Remember to use
System.out.print()
instead ofSystem.out.println()