如何清除控制台



我试过使用\033[H\033[2J.和System.out.flush((;但到目前为止,运气不佳

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
Scanner myObj = new Scanner(System.in);

System.out.println("Hello User,");
System.out.println("Please Enter Username");
String UserName = myObj.nextLine();
System.out.println("Welcome, " + UserName);
System.out.println("Please Enter Your Password");
String Password = myObj.nextLine();
if ("Vaska".equals(Password)){
//Clear Console
System.out.println("You Have Now Gained Access To The Program, Have Fun!");
}else{
//Clear Console
System.out.println("Sorry " + UserName + ", You Have Entered The Wrong Password, Please Restart The Application");
System.exit(0);
}

}

我也尝试过使用System.getProperty((方法,但它总是返回错误。

如果

System.out.print("33[H33[2J");
System.out.flush();

不工作可能意味着您的终端不支持ANSI转义码。

你试过这个吗?

public class ClearScreen{
public  static void main (String [] args){
System.out.println("Hello World");
ClearConsole();
}
public static void ClearConsole(){
try{
String operatingSystem = System.getProperty("os.name") //Check the current operating system

if(operatingSystem.contains("Windows")){        
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "cls");
Process startProcess = pb.inheritIO.start();
startProcess.waitFor();
} else {
ProcessBuilder pb = new ProcessBuilder("clear");
Process startProcess = pb.inheritIO.start();
startProcess.waitFor();
} 
}catch(Exception e){
System.out.println(e);
}
}
}

最新更新