In a Java application I'm using some calls to System.out.println()
. Now I want to find a way to programmatically delete this stuff.
I couldn't find any solution with google, so are there any hints?
In a Java application I'm using some calls to System.out.println()
. Now I want to find a way to programmatically delete this stuff.
I couldn't find any solution with google, so are there any hints?
To clear the Output screen, you can simulate a real person pressing CTRL + L (which clears the output). You can achieve this by using the Robot() class, here is how you can do this:
The easiest ways to do this would be:
You could use cursor up to delete a line, and erase text, or simply overwrite with the old text with new text.
or clear screen
This is standard, but according to wikipedia the Windows console don't follow it.
Have a look: http://www.termsys.demon.co.uk/vtansi.htm
BalusC answer didn't work for me (bash console on Ubuntu). Some stuff remained at the end of the line. So I rolled over again with spaces.
Thread.sleep()
is used in the below snippet so you can see what's happening.where
mul
is a simple method defined as:(Guava's Strings class also provides a similar
repeat
method)Clearing screen in Java is not supported, but you can try some hacks to achieve this.
a) Use OS-depends command, like this for Windows:
b) Put bunch of new lines (this makes ilusion that screen is clear)
c) If you ever want to turn off System.out, you can try this:
I've found that in Eclipse Mars, if you can safely assume that the line you replace it with will be at least as long as the line you are erasing, simply printing '\r' (a carriage return) will allow your cursor to move back to the beginning of the line to overwrite any characters you see. I suppose if the new line is shorter, you can just make up the different with spaces.
This method is pretty handy in eclipse for live-updating progress percentages, such as in this code snippet I ripped out of one of my programs. It's part of a program to download media files from a website.