How to remove material printed on the System.out.println () console?

In a Java application, I use some calls to System.out.println() . Now I want to find a way to programmatically delete this stuff.

I could not find any solution with Google, so are there any clues?

+59
java
Sep 22 2018-11-22T00:
source share
15 answers

You can print the backspace character \b as many times as you previously printed.

 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 does not work flawlessly in the Eclipse console in older versions prior to Mars (4.5). This works great in the command console. See Also How to get backspace \ b to work in the Eclipse console?

+54
Sep 22 '11 at 10:26
source share

Screen cleaning in Java is not supported, but you can try some hacks for this.

a) Use an OS-specific command like this for Windows:

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

b) Place a bunch of new lines (this makes the illusion that the screen is clear)

c) If you want to disable System.out, you can try the following:

 System.setOut(new PrintStream(new OutputStream() { @Override public void write(int b) throws IOException {} })); 
+21
Sep 22 2018-11-22T00:
source share

You can use the cursor to delete a line, erase text, or simply overwrite old text with new text.

 int count = 1; System.out.print(String.format("\033[%dA",count)); // Move up System.out.print("\033[2K"); // Erase line content 

or clear screen

 System.out.print(String.format("\033[2J")); 

This is standard, but according to wikipedia, the Windows console does not follow it.

Take a look: http://www.termsys.demon.co.uk/vtansi.htm

+16
Feb 27 '14 at 23:54
source share

I am using blueJ for Java programming. There is a way to clear the terminal window screen. Try the following: -

 System.out.print ('\f'); 

this will clear everything printed before this line. But this does not work on the command line.

+6
Jun 17 2018-12-17T00:
source share

System.out is a PrintStream , and in itself provides no way to change what gets output. Depending on what this object supports, you may or may not be able to modify it. For example, if you redirect System.out to a log file, you can change this file after the fact. If it goes directly to the console, the text will disappear as soon as it reaches the top of the console buffer, but there is no way to programmatically associate it.

I'm not sure what you hope to achieve, but you might consider creating a PrintStream proxy to filter messages as they appear, rather than trying to delete them after the fact.

+5
Sep 22 2018-11-22T00:
source share

To clear the Output screen, you can simulate a real person by pressing CTRL + L (which clears the output). You can achieve this using the Robot () class, here is how you can do it:

 try { Robot robbie = new Robot(); robbie.keyPress(17); // Holds CTRL key. robbie.keyPress(76); // Holds L key. robbie.keyRelease(17); // Releases CTRL key. robbie.keyRelease(76); // Releases L key. } catch (AWTException ex) { Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex); } 
+4
Aug 21 2018-12-12T00:
source share

There are two different ways to clean the terminal in BlueJ. You can force BlueJ to automatically clear the terminal before each interactive method call. To do this, activate the "Clear screen when calling the method" option in the "Options" menu of the terminal. You can also clear the terminal programmatically from your program. Printing a form character (Unicode 000C) clears the BlueJ terminal, for example:

 System.out.print('\u000C'); 
+4
Mar 07 '16 at 9:09
source share

Just add to BalusC anwswer ...

Reusing System.out.print("\b \b") with a delay gives the same behavior as when uninstalling backspaces in {Windows 7 command console / Java 1.6}

+3
Sep 22 '11 at 23:12
source share

I found that in Eclipse Mars, if you can safely assume that the line you are replacing will be at least as long as the line you are erasing, just printing '\ r' (carriage return) allows your the cursor will return to the beginning of the line to overwrite all the characters that you see. I suppose if the new line is shorter, you can just distinguish between spaces.

This method is pretty convenient in eclipse for real-time progress percentage values, for example, in this piece of code that I pulled from one of my programs. This is part of the program for downloading media files from a website.

  URL url=new URL(link); HttpURLConnection connection=(HttpURLConnection)url.openConnection(); connection.connect(); if(connection.getResponseCode()!=HttpURLConnection.HTTP_OK) { throw new RuntimeException("Response "+connection.getResponseCode()+": "+connection.getResponseMessage()+" on url "+link); } long fileLength=connection.getContentLengthLong(); File newFile=new File(ROOT_DIR,link.substring(link.lastIndexOf('/'))); try(InputStream input=connection.getInputStream(); OutputStream output=new FileOutputStream(newFile);) { byte[] buffer=new byte[4096]; int count=input.read(buffer); long totalRead=count; System.out.println("Writing "+url+" to "+newFile+" ("+fileLength+" bytes)"); System.out.printf("%.2f%%",((double)totalRead/(double)fileLength)*100.0); while(count!=-1) { output.write(buffer,0,count); count=input.read(buffer); totalRead+=count; System.out.printf("\r%.2f%%",((double)totalRead/(double)fileLength)*100.0); } System.out.println("\nFinished index "+INDEX); } 
+3
Aug 22 '15 at 6:06
source share

The easiest way to do this is:

 System.out.println("\f"); System.out.println("\u000c"); 
+1
Aug 21 '15 at 16:05
source share

I found a solution for cleaning the console in the Eclipse IDE. It uses the Robot class. Please see the code below and the title for an explanation:

  import java.awt.AWTException; import java.awt.Robot; import java.awt.event.KeyEvent; public void wipeConsole() throws AWTException{ Robot robbie = new Robot(); //shows the Console View robbie.keyPress(KeyEvent.VK_ALT); robbie.keyPress(KeyEvent.VK_SHIFT); robbie.keyPress(KeyEvent.VK_Q); robbie.keyRelease(KeyEvent.VK_ALT); robbie.keyPress(KeyEvent.VK_SHIFT); robbie.keyPress(KeyEvent.VK_Q); robbie.keyPress(KeyEvent.VK_C); robbie.keyRelease(KeyEvent.VK_C); //clears the console robbie.keyPress(KeyEvent.VK_SHIFT); robbie.keyPress(KeyEvent.VK_F10); robbie.keyRelease(KeyEvent.VK_SHIFT); robbie.keyRelease(KeyEvent.VK_F10); robbie.keyPress(KeyEvent.VK_R); robbie.keyRelease(KeyEvent.VK_R); } 

Assuming you have not changed the default hotkey settings in Eclipse and are importing these Java classes, this should work.

0
May 6 '14 at 14:17
source share

BalusC answer didn't work for me (bash console on Ubuntu). Some things remained at the end of the line. So I rolled over again with spaces. Thread.sleep() used in the snippet below so you can see what happens.

 String foo = "the quick brown fox jumped over the fence"; System.out.printf(foo); try {Thread.sleep(1000);} catch (InterruptedException e) {} System.out.printf("%s", mul("\b", foo.length())); try {Thread.sleep(1000);} catch (InterruptedException e) {} System.out.printf("%s", mul(" ", foo.length())); try {Thread.sleep(1000);} catch (InterruptedException e) {} System.out.printf("%s", mul("\b", foo.length())); 

where mul is a simple method defined as:

 private static String mul(String s, int n) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < n ; i++) builder.append(s); return builder.toString(); } 

( The Guava Strings class also provides a similar repeat method)

0
Nov 18 '15 at 17:15
source share

For the intelligent console, 0x08 worked at 0x08 !

 System.out.print((char) 8); 
0
May 12 '19 at 10:06
source share

I have successfully used the following:

 @Before public void dontPrintExceptions() { // get rid of the stack trace prints for expected exceptions System.setErr(new PrintStream(new NullStream())); } 

NullStream lives in the import com.sun.tools.internal.xjc.util package, so it may not be available for all Java implementations, but it just OutputStream to be simple enough to write your own.

-one
Oct 13 '13 at 15:01
source share

super sticky and does not erase it, but it just sets new lines until it disappears. It is just visual, not something too useful. I only suggest, as I used it for some ASCII art, and it was the best solution.

It also depends on the size of the console, of course.

  System.out.print("Hello"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("world"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); System.out.print("\n"); 
-2
May 15, '16 at 16:00
source share



All Articles