Finding a Method for Incremental Output in Java

In summary: You could use the System.out.println() method to print the running throughput average to the console.
  • #1
Yoss
27
0
I was curious if anyone knows if there are any methods to do the following in java.

I'm writing a terminal based program in java, and it will be doing incremental updates. I want to output to the terminal the percentage done, but to replace the last output. For example:

$>java jprog
Percent completed...0%
...
$>java jprog
Percent completed...5%.

Instead of
$>java jprog
Percent completed...0%
Percent completed...5%
...

I checked the java API's a little, in the PrintStream and OutputStream classes, but couldn't find any methods like that. Anybody know? Thanks.
 
Technology news on Phys.org
  • #2
Standard output by itself cannot do such a thing. To accomplish this, your Java program actually needs to control the terminal window which shows the output. I suggest you look into implementations of "curses" in Java, for example:

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

- Warren
 
  • #3
I'm not sure if your Java app can read termcap/terminfo, but it should be possible to send terminal control characters (or maybe backspace) to move the cursor around. (That's what curses does after all.)
 
  • #4
You could display the information in an alternate manner. For example, you could plot a graph:


#===1===2===3===4===5===6===7===8===9===#

And fill up the line below it one character at a time as it progresses. For example, at 22.5%, it would look like:

#===1===2===3===4===5===6===7===8===9===#
#===1===2=

and the cursor is at the end of the second line, ready to display another character as necessary.

You can make this prettier if you're willing to assume an equal width font.



If you're willing to assume a *NIX-style environment, the character '\r' is a carriage return (without the line feed) -- it positions the cursor at the beginning of the current line. You could use this to achieve the effect you describe. (Backspace would usually work too, as mentioned)
 
  • #5
Thanks for all of your suggestions guys, but I did find out a good way to do it:

'\b'.

:E

edit: oops Nate didn't see your reply!
 
  • #6
Yoss,
Care to share what you did? I am writing a load tool and I want to print a running throughput average to the console.

Thanks.
 

1. What is incremental output in Java?

Incremental output in Java refers to the ability to display output gradually, as it is being generated, rather than waiting for the entire output to be generated before displaying it.

2. Why is incremental output important in Java?

Incremental output can improve the user experience by providing real-time feedback, especially for long-running processes. It also helps to conserve memory and processing power by not storing and displaying large amounts of data all at once.

3. How can I implement incremental output in Java?

There are several ways to implement incremental output in Java, such as using the System.out.print() method instead of System.out.println() to print individual characters or using a StringBuilder to build and print the output gradually.

4. Can I use incremental output for all types of data in Java?

Yes, incremental output can be used for all types of data in Java, including strings, numbers, and objects. It is a general concept that can be applied to any type of output in Java.

5. Are there any downsides to using incremental output in Java?

One potential downside of incremental output is that it may require more code and effort to implement compared to printing the entire output at once. It may also slightly affect performance, but this can usually be mitigated by optimizing the code.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
784
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top