如何删除由System.out.println()打印到控制台的东西?

在Java应用程序中,我使用了一些对System.out.println()调用。 现在我想find一种以编程方式删除这些东西的方法。

谷歌我找不到任何解决scheme,所以有什么提示?

您可以多次打印退格字符\b作为之前打印的字符。

 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"); 

注意:这在Mars(4.5)之前的旧版本的Eclipse控制台中并不完美。 这在命令控制台中可以很好地工作。 另请参见如何让backspace \ b在Eclipse的控制台中工作?

在Java中清除屏幕不被支持,但你可以尝试一些黑客来实现这一点。

a)使用OS-depends命令,如下所示:

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

b)放一堆新的线(这使得屏幕清晰)

c)如果你想closuresSystem.out,你可以试试这个:

 System.setOut(new PrintStream(new OutputStream() { @Override public void write(int b) throws IOException {} })); 

您可以使用光标删除一行,删除文本,也可以用旧文本覆盖旧文本。

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

或清除屏幕

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

这是标准的,但根据维基百科的Windows控制台不遵循它。

看看: http : //www.termsys.demon.co.uk/vtansi.htm

System.out是一个PrintStream ,本身并不提供任何方式来修改输出。 根据支持该对象的内容,您可能会也可能不能修改它。 例如,如果将System.outredirect到日志文件,则可以在事后修改该文件。 如果直接进入控制台,文本一旦到达控制台缓冲区的顶部就会消失,但是没有办法以编程的方式混淆它。

我不确定你希望完成什么,但你可能要考虑创build一个代理PrintStream来筛选消息,因为他们得到输出,而不是试图在事后删除它们。

我正在使用blueJ进行java编程。 有一种方法可以清除它的terminal窗口的屏幕。 尝试这个:-

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

这将清除此行之前所印的任何内容。 但是这在命令提示符下不起作用。

要清除输出屏幕,可以模拟真实的人按CTRL + L (清除输出)。 你可以通过使用Robot()类来实现这一点,下面是你可以这样做的:

 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); } 

只是添加到BalusC的anwswer …

重复地调用System.out.print("\b \b")会产生与我们在{Windows 7命令控制台/ Java 1.6}中返回空格时完全相同的行为

我发现在Eclipse Mars中,如果你可以安全地假定你replace的行至less和你擦除的行一样长,只要打印“\ r”(一个回车符)就可以让你的光标移动移回到行首,覆盖你看到的任何字符。 我想如果新的路线比较短,你可以用空格来弥补。

在eclipse中,这个方法在实时更新进度百分比方面非常方便,比如在我从一个程序中剥离出的代码片段中。 它是从网站上下载媒体文件的程序的一部分。

  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); } 

在BlueJ中清除terminal有两种不同的方法。 每次交互式方法调用之前,您都可以让BlueJ自动清除terminal。 为此,请激活terminal的“选项”菜单中的“方法调用清除屏幕”选项。 您也可以从程序中以编程方式清除terminal。 打印换页符(Unicode 000C)将清除BlueJterminal,例如:

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

我发现了一个在Eclipse IDE中擦除控制台的解决scheme。 它使用机器人类。 请参阅下面的代码和标题说明:

  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); } 

假设您没有更改Eclipse中的默认热键设置并导入这些Java类,这应该工作。

最简单的方法是:

 System.out.println("\f"); System.out.println("\u000c"); 

BalusC答案不适合我(Ubuntu的bash控制台)。 一些东西仍然在线的末尾。 所以我又用空格翻了一遍。 Thread.sleep()在下面的代码片段中使用,所以你可以看到发生了什么。

 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())); 

其中mul是一个简单的方法定义为:

 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(); } 

( 番石榴的string类也提供了类似的repeat方法)

我已经成功地使用了以下内容:

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

NullStream位于import com.sun.tools.internal.xjc.util包中,所以在所有的Java实现中都不可用,但它只是一个OutputStream ,应该足够简单,可以编写自己的。

超级俗气,并没有消除它,但它只是把新的线路,直到它消失。 这只是为了视觉,而不是太有用。 我只是build议,因为我正在使用一些ASCII艺术,这是最好的解决scheme。

当然这也取决于控制台的大小。

  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");