使用Java在命令行上打印的明文

我如何清除使用Java在命令行中打印的文本? 我想在打印后清除Text1并用Text2覆盖。 我搜索并找到此代码,但它不起作用。

 public class className { public static void main(String[] args) throws IOException { System.out.println("Text1"); Runtime.getRuntime().exec("cls"); System.out.println("Text2"); } } 

您可以通过打印\b执行此操作:

 System.out.print("Text1"); System.out.print("\b\b\b\b\b"); System.out.print(" "); 

请注意,这在Eclipse控制台中不起作用。

您只需使用’\ r’(回车)转义序列即可。

 System.out.print("Text1"); System.out.print("\rText2"); 

只会打印’Text2′

试试这个解决方案

 new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); 

我正在使用Windows 8,这个解决方案对我有用。 希望它对你也有好处。 🙂