java中清除屏幕的命令
Java 中的哪个命令可以让您清除命令行应用程序中的控制台?
What command in Java will let you clear the console in a command-line application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Java 中的哪个命令可以让您清除命令行应用程序中的控制台?
What command in Java will let you clear the console in a command-line application?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
我认为OP想要的是清除屏幕并将光标移动到起始位置。为此尝试:
I think what the OP wants is to clear the screen and move the cursor to the home position. For that try:
这取决于您的控制台,但如果它支持 ANSI 转义序列,请尝试此操作。
It depends on your console but if it supports ANSI escape sequences, then try this..
运行此示例程序:它演示了如何使用转义序列清除控制台并将光标重新定位到位置 X=1, Y=1。
我在几个 Linux 终端上测试了它。不知道Windows下是否可以运行。
也许你可以告诉我;)
阅读这篇文章了解转义序列。
Run this sample program: it demonstrates how to clear the console using an escape sequence and reposition the cursor to position X=1, Y=1.
I tested it on several Linux terminals. Don't know, if it works under Windows.
Perhaps you can tell me ;)
Read this article about escape sequences.
清除对我有用的 bash:
clearing for bash that is working for me:
清除屏幕通常需要发送特定于应用程序运行所在的屏幕/终端的特殊控制序列。选项:
如果您知道自己将始终在特定终端下运行,并且可以找到正确的控制序列来清除该终端的屏幕,则只需输出这些序列即可。如果您告诉我们屏幕,我们也许能够告诉您顺序(可能有点 ANSI/VT100/VT220 兼容)。
从外部确保您的应用程序始终在所需的终端中运行,例如启动应用程序的脚本会在所需的终端中启动应用程序。然后输出必要的字符序列以清除屏幕。
通过使用终端仿真库来控制终端,即您的应用程序现在是一个窗口应用程序,它创建一个终端窗口屏幕供用户使用。然后,您可以控制正在模拟的终端,并且知道需要什么控制序列。
使用终端库(例如历史悠久的curses库)来检测终端并为其功能提供统一的接口。请参阅这个问题:
什么是好的Java,类似curses,终端应用程序库?
通过在屏幕上写入一堆行来伪造它。
Clearing a screen generally requires sending special control sequences specific to the screen/terminal that your application is running under. Options:
If you know you will always running under a specific terminal and can find the proper control sequences to clear the screen for that terminal, just output those sequences. If you tell us the screen, we may be able to tell you the sequence (its likely somewhat ANSI/VT100/VT220 -compatible).
Externally ensure your app is always run in a desired terminal, e.g. a script to start your app starts the app in the desired terminal. Then output the necessary character sequence to clear the screen.
Take control of the terminal by using a terminal emulation library, i.e. you app is now a windowing app that creates a terminal window screen for the user to use. You then control what terminal you are emulating and will know what control sequences are needed.
Use a terminal library (e.g. like the historic
curses
library) that detects the terminal and provides an uniform interface to its features. See this question:What's a good Java, curses-like, library for terminal applications?
Fake it by writing a bunch of lines to the screen.
如果上述解决方案都不适合您(如我的情况),请尝试以下解决方案:
我使用的是 Windows 8,并且此解决方案适合我。希望它对你也一样。 :)
If none of the above solutions works for you( as in my case ), try this solution:
I'm using Windows 8 and this solution worked for me. Hope it does to you as well. :)
总是有明显的(而且混乱的)..
There is always the obvious (and kludgy)..
打印指定的字符串,然后将光标移动到下一行。
打印指定的字符串并将光标保留在该字符串之后。
要解决上面提到的光标位于控制台第二行的问题,请使用
print
而不是println
。prints the specified string and then moves the cursor to the next line.
prints the specified string and leaves the cursor immediately after that string.
To solve the problem, identified above, of the cursor being on the second line of the console, use
print
instead ofprintln
.我在 BlueJ 中做到了这一点,效果非常好:尝试
System.out.print("\f");
I did this in BlueJ and it worked perfectly: Try
System.out.print("\f");
据我所知,Windows 10 的命令窗口 cmd.exe 本身并不支持 ANSI ESC 序列,尽管有传言。要使:
方法起作用,您需要一个支持 ANSI 的命令行模拟器,例如 ConEmu64。
To my knowledge Windows 10's Command Window cmd.exe does not natively support ANSI ESC sequences, despite the rumors. To have the:
method work you need a command line emulator that supports ANSI, such as ConEmu64.
有两种非常简单的方法可以解决这个问题,第一个是暴力选项:
然而,这样做的问题是它只是伪清除屏幕,你可以向上滚动查看数据,但不要害怕,还有另一种方法方式!
瞧!这应该可以解决问题,尽管在屏幕清除后您的光标将位于控制台的第二行。
There are two very simple ways to solve this, the first is the brute force option:
The problem with this however is that it only pseudo clears the screen, you can scroll up to see the data, but don't fear, there is another way!
Voila! That should do the trick, although your cursor will be situated on the second line of the console after the screen is cleared.