You are on page 1of 1

java - How to delete stu printed to console by ... https://stackoverow.com/questions/7522022/ho...

How to delete stuff printed to console by System.out.println()?

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?

java

edited Aug 25 '16 at 13:23 asked Sep 22 '11 at 22:03


Zim-Zam O'Pootertoot anon
14.6k 2 23 59

3 What do you mean by "delete"? Do you mean it created a file somewhere and you want to delete that? Or
you want the program not to output the data to the console? Or... something else? You haven't provided
anywhere near enough information for anyone to understand what you want, much less help you. Explain
how you invoke the program, and where the "stuff" you want deleted is. Jim Garrison Sep 22 '11 at
22:07

3 You mean you want to delete System.out.println() calls from your code? OR you want that code
untouched, but nothing written to sysout although code executes System.out.println()? Unix/Windows?
Kashyap Sep 22 '11 at 22:08

1 Lesson learned: Don't print to standard out :D Next time use a Logger Nate W. Sep 22 '11 at 22:09

I want to delete the content from the console, the terminal, the screen. anon Sep 22 '11 at 22:12

1 Clear the content , i guess is what he want Rami Shareef Sep 22 '11 at 22:13

14 Answers

You could print the backspace character \b as many times as the characters which were printed
before.

System.out.print("hello");
Thread.sleep(1000); // Just to give the user a chance to see "hello".
System.out.print("\b\b\b\b\b");
System.out.print("world");

Note: this doesn't work flawlessly in Eclipse console in older releases before Mars (4.5). This
works however perfectly fine in command console. See also How to get backspace \b to work in
Eclipse's console?

edited May 23 at 12:34 answered Sep 22 '11 at 22:26


Community BalusC
1 1 735k 242 2721
2890

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:

Runtime.getRuntime().exec("cls");

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:

System.setOut(new PrintStream(new OutputStream() {


@Override public void write(int b) throws IOException {}
}));

edited Sep 22 '11 at 22:23 answered Sep 22 '11 at 22:09


smas
15.3k 10 37 60

Good suggestions. For (c) why do you throw an IOException? As I understand it, the PrintStream will
swallow the IOException. emory Sep 22 '11 at 22:47

@emory: Actually it isn't necessary here, I've just created the stub in eclipse and it had inserted me this
(it's exactly the same definition like in OutputStream) smas Sep 22 '11 at 22:53

@emory - a more cogent reason to leave out the throws is that the method body doesn't throw the
exception. But this is all nitpicking. Stephen C Sep 22 '11 at 23:36

Upvote dude, But how do you switch System.setOut, back to normal ? I did not find a System.getOut
taxeeta Jul 21 '13 at 11:20

1 de 1 25/6/17 22:00

You might also like