clear screen option in java [duplicate]

Is there any option to clear the screen in java as clrscr() in C.


Solution 1:

As dirty hacks go, I like msparer's solution. An even dirtier method that I've seen used (I would never do this myself. I swear. Really.) is to write a bunch of newlines to the console. This doesn't clear the screen at all, but creates the illusion of a clear screen to the user.

char c = '\n';
int length = 25;
char[] chars = new char[length];
Arrays.fill(chars, c);
System.out.print(String.valueOf(chars));

Solution 2:

If you're talking about a console application, then there isn't a clear screen option AFAIK. A quite dirty option would be to invoke the clear screen command of the underlying OS.

Then it's something like

Runtime.getRuntime().exec("cls");

for Windows or

Runtime.getRuntime().exec("clear");

for a load of other OS. You can find out the OS with System.getProperty("os.name").

Solution 3:

If you're talking about the console, then no. Writing to the console is just a special case of an output stream. Output streams don't know anything about the screen, as they can be just as easily redirected to a file or another system device.