+12 votes
4 views
in Java by (920 points)
I have output in my Java console from running an application, how I can i clear it using code ?

2 Answers

+13 votes
by (13.2k points)

In java, there is a function to clear console and not a command ,like in C++ there is a command :

system("CLS");

In java, there is a function :

public static void clearScreen() {  

   System.out.print("\033[H\033[2J");  

   System.out.flush();  

}

+1 vote
by (31.4k points)
edited by

Create a method in your class like this:

public static void clrscr(){

    //Clears Screen in java

    try {

        if (System.getProperty("os.name").contains("Windows"))

            new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();

        else

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

    } catch (IOException | InterruptedException ex) {}

}

I haven’t checked this for Linux But this definitely works in Windows.

Want to learn Java end to end! Here's an informative video on Java:

Also, there is an alternate method is to write this code in clrscr():

for(int i = 0; i < 80*300; i++) // Default Height of cmd is 300 and Default width is 80

    System.out.print("\b"); // Prints a backspace

Related questions

0 votes
1 answer
asked Jul 13, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...