java中的清除屏幕选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1682212/
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 option in java
提问by user161004
Is there any option to clear the screen in java as clrscr() in C.
是否有任何选项可以将 java 中的屏幕清除为 C 中的 clrscr()。
采纳答案by Bill the Lizard
As dirty hacks go, I like msparer's solution. An even dirtier method that I've seen used (I would never do this myself. I swear. Really.) is to write a bunch of newlines to the console. This doesn't clear the screen at all, but creates the illusion of a clear screen to the user.
随着肮脏的黑客攻击,我喜欢 msparer 的解决方案。我见过使用的更脏的方法(我自己永远不会这样做。我发誓。真的。)是向控制台写入一堆换行符。这根本不会清除屏幕,而是为用户创造了清晰屏幕的错觉。
char c = '\n';
int length = 25;
char[] chars = new char[length];
Arrays.fill(chars, c);
System.out.print(String.valueOf(chars));
回答by James Cronen
If you're talking about the console, then no. Writing to the console is just a special case of an output stream. Output streams don't know anything about the screen, as they can be just as easily redirected to a file or another system device.
如果你在谈论控制台,那么不。写入控制台只是输出流的一个特例。输出流对屏幕一无所知,因为它们可以很容易地重定向到文件或其他系统设备。
回答by msp
If you're talking about a console application, then there isn't a clear screen option AFAIK. A quite dirtyoption would be to invoke the clear screen command of the underlying OS.
如果您在谈论控制台应用程序,则没有清晰的屏幕选项 AFAIK。一个相当肮脏的选择是调用底层操作系统的清除屏幕命令。
Then it's something like
然后它就像
Runtime.getRuntime().exec("cls");
for Windows or
对于 Windows 或
Runtime.getRuntime().exec("clear");
for a load of other OS. You can find out the OS with System.getProperty("os.name")
.
用于加载其他操作系统。您可以通过System.getProperty("os.name")
.
回答by ChssPly76
For Windows, Java Console APIproject provides functionality to determine console size and set cursor position. Clearing the screen is trivial with that. It's a version 0.2 now so it's not exactly production ready, but it works.
对于 Windows,Java 控制台 API项目提供了确定控制台大小和设置光标位置的功能。清除屏幕是微不足道的。它现在是 0.2 版,所以还没有完全准备好生产,但它可以工作。
Alternatively, you can simply print out some new lines via System.out.println()
. 640 should be enough for everybody :-) It's not the same as clearing screen, but for user's intents and purposes it'd do.
或者,您可以简单地通过 打印出一些新行System.out.println()
。640 对每个人来说都足够了:-) 这与清除屏幕不同,但对于用户的意图和目的来说,它会这样做。
回答by LB40
回答by jitter
For any console which supports ANSI escapes the following would work (would e.g. work in Win98 console).
对于任何支持 ANSI 转义的控制台,以下都可以工作(例如在 Win98 控制台中工作)。
private final String ANSI_CLS = "\u001b[2J";
....
System.out.print(ANSI_CLS);
System.out.flush();
...
Starting with Win NT this won't work anymore and you can either
从 Win NT 开始,这将不再起作用,您可以
- Do a JNI call (e.g. like here: Java: Clear console and control attributes
- Or write out a bunch of empty lines
- 做一个 JNI 调用(例如像这里:Java:清除控制台和控制属性
- 或者写出一堆空行
Otherwise you are out of luck.
否则你就倒霉了。
And btw. you must keep in mind that System.out
and System.err
don't have to be console they could be set to what ever (writing into a file e.g.) an usecase where clearing the screen wouldn't make any sense at all.
顺便说一句。您必须记住,System.out
并且System.err
不必是控制台,它们可以设置为什么(写入文件,例如)一个用例,在那里清除屏幕根本没有任何意义。
回答by dfa
you should give a try with JNA and try mapping native libraries:
您应该尝试使用 JNA 并尝试映射本机库:
- on linux you must map C functions from ncurseslibrary
- on windows you must map functions from both msvcrtand kernel32, as clearly stated here
- 在 linux 上,您必须从ncurses库中映射 C 函数
- 在Windows上,必须从两个映射功能的msvcrt和KERNEL32,如明确规定这里
PS
聚苯乙烯
let me known if you need some sample code
如果您需要一些示例代码,请告诉我
回答by dfa
Jansiis an excellent workaround. I am an amateur coder and Jansi is easy to setup especially with Eclipse.
Jansi是一个很好的解决方法。我是一名业余编码员,Jansi 很容易设置,尤其是使用 Eclipse。
The following is a link to the homepage of Jansi:
以下是Jansi主页的链接:
The following is a link to a site containing a code as a demonstration of AnsiConsole class contained in the Jansi package:
以下是包含代码的站点的链接,作为 Jansi 包中包含的 AnsiConsole 类的演示:
回答by Dhickey90
To clear the screen just type:
要清除屏幕,只需键入:
System.out.print('\u000C');
回答by gon1332
You can also try ANSI Escape Codes:
您还可以尝试 ANSI 转义码:
If your terminal support them, try something like this:
如果您的终端支持它们,请尝试以下操作:
System.out.print("3[2J3[1;1H");
You can include \0333[1;1H
to be sure if \0333[2J
does not move the cursor in the upper left corner.
您可以包括\0333[1;1H
以确保\0333[2J
不移动左上角的光标。
More specifically:
进一步来说:
033
is the octal ofESC
2J
is for clearing the entire console/terminal screen1;1H
moves the cursor to row 1 and column 1
033
是八进制ESC
2J
用于清除整个控制台/终端屏幕1;1H
将光标移动到第 1 行和第 1 列