Java

Associate
Joined
26 Jan 2006
Posts
1,502
Hey Java gurus,

Is it possible to "clear" the console window when a text-based application is running?

Basically, I need to start "fresh" after a choice etc.

I could always print a couple of carriage returns just to get some empty lines, but this assumes the console window size is fixed (which is not).

Any ideas?

Thanks.
 
Just had a quick google and it doesn't look like there is a proper way of doing it, suppose a workaround would be:

Code:
void public clear () 
{
int i = 0;

for(i=0;i<300;i++)
{
System.out.println("\n");
}

}

Don't think it would be too many clock cycles, Also you can optimise it by reducing i to the buffer size of your screen.
 
Hamish's way would work but it's a bit of a kludge. If you really needed the ability to clear the console then you would need to build a basic GUI with a console rather than running your program as a console app.
 
Well you could also call a platform specific command such as 'CLS' using runtime exec. However that is also really bad practice. :p
 
Back
Top Bottom