在 Java 控制台应用程序中使用 Windows“cls”命令清除屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19252496/
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
Clear screen with Windows "cls" command in Java console application
提问by greatmastermario
I am working on a game that involves clearing the screen after each turn for readability. The only problem is I cannot use the Windows command prompt-based "cls" command and it does not support ANSI escape characters. I used Dyndrilliac's solution on the following page but it resulted in an IOException:
我正在开发一个游戏,该游戏涉及在每回合后清除屏幕以提高可读性。唯一的问题是我不能使用基于 Windows 命令提示的“cls”命令,它不支持 ANSI 转义字符。我在下一页使用了 Dyndrilliac 的解决方案,但它导致了 IOException:
Replacing "cls" with "cmd \C cls" only opened a new command prompt, cleared it, and closed it without accessing the current console. How do I make a Java program running through Windows Command Prompt access the command prompt's arguments and use them to clear its output?
用“cmd \C cls”替换“cls”只会打开一个新的命令提示符,清除它,然后在不访问当前控制台的情况下关闭它。如何让通过 Windows 命令提示符运行的 Java 程序访问命令提示符的参数并使用它们清除其输出?
采纳答案by Sean
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
Solved here: Java: Clear the console
在这里解决:Java:清除控制台
I realize this is an old post, but I hate when I find questions with responses of never mind i got it, or it just dies off. Hopefully it helps someone as it did for me.
我意识到这是一个旧帖子,但我讨厌当我发现问题的回答不介意我得到它,或者它就消失了。希望它能像对我一样帮助某人。
Keep in mind it won't work in eclipse, but will in the regular console. take it a step further with if you're worried about cross OS:
请记住,它在 eclipse 中不起作用,但可以在常规控制台中运行。如果您担心跨操作系统,请更进一步:
final String os = System.getProperty("os.name");
if (os.contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
回答by Ravi Thapliyal
There's pretty much nothing in the console related API to do a clear screen. But, you can achieve the same effect through println()
s. A lot of putty clients clear the page like that and then scroll up.
控制台相关的 API 中几乎没有任何内容可以做一个清晰的屏幕。但是,您可以通过println()
s实现相同的效果。许多腻子客户端像这样清除页面,然后向上滚动。
private static final int PAGE_SIZE = 25;
public static void main(String[] args) {
// ...
clearScreen();
}
private static void clearScreen() {
for (int i = 0; i < PAGE_SIZE; i++) {
System.out.println();
}
}
回答by Ravi Thapliyal
Create a batch file to clear the cmd screen and run your java program
创建一个批处理文件来清除 cmd 屏幕并运行您的 java 程序
Step 1. Create a file with extension .bat Step 2. So your code in batch file will be
步骤 1. 创建一个扩展名为 .bat 的文件 步骤 2. 所以你在批处理文件中的代码将是
Cls Cd desktop // path Javac filename.java // compiling Java desk // running
cls cd desktop // 路径 Javac filename.java // 编译 Java 桌面 // 运行
By doing this....you can clear the screen during run time
通过这样做....您可以在运行时清除屏幕
回答by Ankush Mundhra
public static void clrscr(){
//Clears Screen in java
try {
if (System.getProperty("os.name").contains("Windows"))
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
else
Runtime.getRuntime().exec("clear");
} catch (IOException | InterruptedException ex) {}
}