如何清除Java中的控制台? [英] How to Clear Console in Java?

查看:181
本文介绍了如何清除Java中的控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从很长一段时间以来,我一直在为此拉头发.我已经研究了一个小时,以了解如何清除Java中的控制台.

I have been pulling my hair for this since quite long time. I have researched for an hour on how to clear a console in Java.

我发现的全部是通过打印一堆行或执行此行来进行肮脏的黑客攻击

All I found was dirty hacking either by printing a bunch of lines or executing this

Runtime.getruntime.exec("cls/clear");

但是,似乎没有什么对我有用.真的没有像C(clrscr();)一样清除Java中控制台的方法.没有任何外部库可以实现此目的.

However, nothing seems to be working for me. Isn't there really any a way of clearing the console in Java like in C (clrscr();). Isn't there any external library by which this can be achieved.

请告知我是否有人在使用适当的功能,库等之前进行过此操作,而不是进行恶意黑客入侵.

Please let me know if anyone has ever done this before using a proper function, library etc. instead of dirty hacking.

推荐答案

如果您的终端支持 ANSI转义码,这将清除屏幕并将光标移至第一行,第一列:

If your terminal supports ANSI escape codes, this clears the screen and moves the cursor to the first row, first column:

System.out.print("\033[H\033[2J");
System.out.flush();

这几乎可以在所有UNIX终端和终端仿真器上使用. Windows cmd.exe不会解释ANSI转义码.

This works on almost all UNIX terminals and terminal emulators. The Windows cmd.exe does not interprete ANSI escape codes.

这篇关于如何清除Java中的控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆