我有一个继承自Thread类的类。在它的run方法中有一个System.out.println
语句。在执行这个打印语句之前,我想清除控制台。我该怎么做?
我尝试过
Runtime.getRuntime().exec("cls"); // and "clear" too
并且
System.out.flush();
但是两者都没有起作用。
我有一个继承自Thread类的类。在它的run方法中有一个System.out.println
语句。在执行这个打印语句之前,我想清除控制台。我该怎么做?
我尝试过
Runtime.getRuntime().exec("cls"); // and "clear" too
并且
System.out.flush();
cls
命令只适用于Windows系统。Runtime.getRuntime().exec("cls");
Mac:
Runtime.getRuntime().exec("clear");
flush
简单地强制将所有缓冲输出立即写入。它不会清除控制台。
编辑 抱歉,这些清除只适用于实际使用控制台的情况。在eclipse中,没有办法以编程方式清除控制台。您必须放置空格或单击清除按钮。
因此,您真的只能使用类似这样的东西:
for(int i = 0; i < 1000; i++)
{
System.out.println("\b");
}
final String operatingSystem = System.getProperty("os.name");
if (operatingSystem .contains("Windows")) {
Runtime.getRuntime().exec("cls");
}
else {
Runtime.getRuntime().exec("clear");
}
for(int clear = 0; clear < 1000; clear++) {
System.out.println("\b") ;
}
这是我在一个网站上找到的例子,希望它能起作用:
public static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}