In a Java program, clearing the terminal or console screen is possible using ANSI escape codes provided the terminal is able to support it:
public class ClearConsole {
public static void main(String[] args) {
clearConsole();
System.out.println("Console cleared!");
}
public static void clearConsole() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}
Note:
This is only applicable if the console in the windows is a UNIX system like Linux or MAC. In the Windows command prompt, it may not work in the proper manner. If an answer is required that can work in several operating systems, doing the following would suffice: print several new lines.
public static void clearConsole() {
for (int i = 0; i < 50; ++i) System.out.println();
}
This approach, however, does not really clear the output buffer, but rather gives the impression of clearing the console.