开发者

In Scala or Java, how to print a line to console replacing its previous content instead of appending?

开发者 https://www.devze.com 2023-01-14 17:13 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消