如何在 Java 中清除控制台 - Eclipse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37301530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear console in Java - Eclipse
提问by Chandra Shekhar
How to clear the console after a few statements have been executed/printed in Eclipse. I have used flush() but didn't work though. Just posting the sample code.
在 Eclipse 中执行/打印一些语句后如何清除控制台。我已经使用了 flush() 但没有用。只需发布示例代码。
System.out.println("execute ");
System.out.println("these set of lines ");
System.out.println("first");
Thread.sleep(2000); // just to see the printed statements in the console
System.out.flush(); // it is not clearing the above statements
回答by Chandra Shekhar
It depends on your console but if it supports ANSI escape sequences, then try this..
这取决于你的控制台,但如果它支持 ANSI 转义序列,那么试试这个..
final static String ESC = "3[";
System.out.print(ESC + "2J");
Also, you can print multiple end of lines ("\n") and simulate the clear screen. At the end clear, at most in the unix shell, not removes the previous content, only moves it up and if you make scroll down can see the previous content.
此外,您可以打印多个行尾(“\n”)并模拟清晰的屏幕。最后清楚,最多在unix shell中,不会删除以前的内容,只能向上移动,如果向下滚动可以看到以前的内容。
Here is a sample code:
这是一个示例代码:
for (int i = 0; i < 50; ++i) System.out.println();
A third option:
第三种选择:
import java.io.IOException;
public class CLS {
public static void main(String... arg) throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
}
回答by E-Riz
System.out
is a Stream
, it makes no sense to clear a stream.
System.out
是 a Stream
,清除流没有意义。
Eclipse's Consoleis a view that renders that stream and has richer capabilities (including the ability to clear its visible buffer of the stream's content) but the only way to access that is if you were writing an Eclipse plug-in; general Java code has no knowledge of Eclipse's Console view.
Eclipse 的控制台是一个呈现该流的视图,并具有更丰富的功能(包括清除流内容的可见缓冲区的能力),但访问它的唯一方法是编写 Eclipse 插件;一般 Java 代码不了解 Eclipse 的控制台视图。
You might be able to achieve something moderately useful via some hacks, but I would not recommend relying on it. Usage of System.out
in production code is highly discouraged anyway; use a Logger (such as slf4jand logback) instead. And consider what you're really trying to achieve.
您也许可以通过一些 hacks获得一些有用的东西,但我不建议依赖它。System.out
无论如何,强烈不鼓励在生产代码中使用;改用 Logger(例如slf4j和logback)。并考虑您真正想要实现的目标。
回答by KVS
In case you are talking about eclipse (java console, there is mvn console and svn console as well) then you might want to work with preferences like below:
如果您在谈论 eclipse(java 控制台,也有 mvn 控制台和 svn 控制台),那么您可能希望使用如下首选项:
- Go to Window ? Preferences to show the Preferences dialog
- Go to Run/debug and expand it
- Select Console
- Here you can now set the console related properties.
- 去窗口?显示首选项对话框的首选项
- 转到运行/调试并展开它
- 选择控制台
- 您现在可以在此处设置与控制台相关的属性。
回答by howlger
The Eclipse Console does not supportthe interpretation of the clear screenand other ANSI escape sequenceswhich would be required for that. Also, the ANSI Escape in ConsoleEclipse plug-in does not support clear screen.
在Eclipse的控制台不支持该解释清屏等ANSI转义序列,其将需要这一点。此外,控制台Eclipse 插件中的ANSI Escape不支持clear screen。
In the upcoming Eclipse IDE 2019-12 (4.14)which will be released on December 18, 2019, the interpretation of the backslash(\b
) and carriage return(\r
) characters can be enabled in the preferences (see Eclipse 4.14 - New and Noteworthy - Control character interpretation in Console View):
在即将于 2019 年 12 月 18 日发布的即将发布的 Eclipse IDE 2019-12 (4.14) 中,可以在首选项中启用对反斜杠( \b
) 和回车( \r
) 字符的解释(参见Eclipse 4.14 - New and Noteworthy - Control控制台视图中的字符解释):