简体   繁体   中英

Java: Display changing text on output

Currently, my program prints to System.out :

Processing line 1 of 3...
Processing line 2 of 3...
Processing line 3 of 3...

Is there any way to have it replace the line numbers, instead of printing a new line?

Well there are some hacks you can try, but none of them are guaranteed to work everywhere. Escape characters such as \\b (backspace) can erase the last character and \\r (replace) can erase the last output.

It may or may not work (depending on IDE), but you could try:

System.out.print("Hello");
Thread.sleep(1000);
System.out.print("\rWorld");

However, generally a Swing box should be used (dynamically updating GUI) to measure progress. See Progress Bars .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM