In Scala or Java, how to print a line to console replacing its previous content instead of appending?
A Scala application does some data processing. It would be nice to show processing progress in percents overwriting previous value on change rather than appendin开发者_运维技巧g a new value to what's already displayed. How to acheive this effect? I use Scala 2.8 on Linux.
Your easiest options are to use the carriage return without linefeed "\r"
or the backspace character "\u0008"
. If you backspace, you need to keep track of how many characters you've emitted so you can back up enough (and blank out any excess). If you carriage return, you need to rewrite the entire line (and write spaces to blank out any excess).
If you were programming in C/C++, the answer would unequivocably be "ncurses".
There is a curses implemention in Java you might be interested in:
Java Curses
精彩评论