如何在Java-Eclipse中清除控制台
如何在Eclipse中执行/打印一些语句后清除控制台。我用过flush()但没用。只是发布示例代码如何在Java-Eclipse中清除控制台,java,eclipse,console,flush,Java,Eclipse,Console,Flush,如何在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(); //
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
这取决于您的控制台,但如果它支持ANSI转义序列,请尝试此
final static String ESC = "\033[";
System.out.print(ESC + "2J");
此外,您还可以打印多行结尾(“\n”)并模拟清晰屏幕。最后,在unixshell中,最多是清除,不删除以前的内容,只向上移动它,如果您使scroll down可以看到以前的内容
下面是一个示例代码:
for (int i = 0; i < 50; ++i) System.out.println();
System.out
是一个流
,清除流毫无意义
Eclipse的控制台是一个呈现该流的视图,具有更丰富的功能(包括清除流内容的可见缓冲区的功能),但访问该视图的唯一方法是编写Eclipse插件;通用Java代码不了解Eclipse的控制台视图
您可能可以通过实现一些适度有用的功能,但我不建议依赖它。无论如何,强烈反对在生产代码中使用
System.out
;改用记录器(如和)。考虑一下你真正想要达到的目标。 < P>如果你在谈论Eclipse(java控制台,还有MVN控制台和VPN控制台),那么你可能想使用下面的选项:
Eclipse控制台不支持对清晰屏幕和其他需要的ANSI转义序列的解释。此外,Eclipse插件不支持清晰屏幕 在即将于2019年12月18日发布的Eclipse IDE2019-12(4.14)中,可以在首选项中启用反斜杠(
\b
)和回车符(\r
)的解释(请参阅):
我认为您正在尝试使用java清除Eclipse控制台。是这样吗?如果是这样,我认为您可能不会以这种方式影响应用程序控制台。除非您正在开发一些在eclipse中使用的工具。为什么您需要这个?是的,这仅限于eclipse而不是任何生产应用程序
import java.io.IOException;
public class CLS {
public static void main(String... arg) throws IOException, InterruptedException {
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
}
}