Is there any option to clear the screen in java as clrscr() in C.
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
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.
You can also try ANSI Escape Codes:
If your terminal support them, try something like this:
System.out.print("\033[2J\033[1;1H");
You can include \0333[1;1H
to be sure if \0333[2J
does not move the cursor in the upper left corner.
More specifically:
033
is the octal of ESC
2J
is for clearing the entire console/terminal screen1;1H
moves the cursor to row 1 and column 1If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With