java 在Cmd命令行实现清屏

清屏代码:

/**   
 * @throws IOException  
 */  
public static void cls() throws IOException
{
	new ProcessBuilder("cmd", "/C", "cls")
	.inheritIO()
	.start();
}

测试:

import java.io.IOException;
public class ConsoleCLS
{
	public static void main(String[] args)
			throws IOException, InterruptedException
	{
		for (int i = 0; i < 20; i++)
		{
			System.out.println("%%%%%%  " + i + "   %%%%%");
		}
		// 睡眠三秒
		Thread.currentThread().sleep(1000 * 3);
		// 控制台清屏
		cls();
	}
	/**
	 * @throws IOException
	 */
	public static void cls() throws IOException
	{
		new ProcessBuilder("cmd", "/C", "cls").inheritIO().start();
	}
}

上面的代码只在CMD命令行中有效,在Eclipse中是无效的。

在eclipse中复制地址,然后进入目录中,打开命令行测试:

1.带包编译:javac -d . 类名.java

2.带包运行:java 包名.类名


三秒后:

可以看到刚才控制台中打印的已经被清理掉了。

清屏代码有效,具体原理我还没去弄清,这里先保存一下。




猜你喜欢

转载自blog.csdn.net/qq_21808961/article/details/80445365