For any console which supports ANSI escapes the following would work (would e.g. work in Win98 console).
private final String ANSI_CLS = "\u001b[2J";
....
System.out.print(ANSI_CLS);
System.out.flush();
...
Starting with Win NT this won't work anymore and you can either
- Do a JNI call (e.g. like here: Java: Clear console and control attributes
- Or write out a bunch of empty lines
Otherwise you are out of luck.
And btw. you must keep in mind that System.out
and System.err
don't have to be console they could be set to what ever (writing into a file e.g.) an usecase where clearing the screen wouldn't make any sense at all.