Java: Clear the console

后端 未结 14 2686
青春惊慌失措
青春惊慌失措 2020-11-21 23:26

Can any body please tell me what code is used for clear screen in Java? For example in C++

system(\"CLS\");

What code is used in Java for

相关标签:
14条回答
  • 2020-11-22 00:09

    If you want a more system independent way of doing this, you can use the JLine library and ConsoleReader.clearScreen(). Prudent checking of whether JLine and ANSI is supported in the current environment is probably worth doing too.

    Something like the following code worked for me:

    import jline.console.ConsoleReader;
    
    public class JLineTest
    {
        public static void main(String... args)
        throws Exception
        {
            ConsoleReader r = new ConsoleReader();
    
            while (true)
            {
                r.println("Good morning");
                r.flush();
    
                String input = r.readLine("prompt>");
    
                if ("clear".equals(input))
                    r.clearScreen();
                else if ("exit".equals(input))
                    return;
                else
                    System.out.println("You typed '" + input + "'.");
    
            }
        }
    }
    

    When running this, if you type 'clear' at the prompt it will clear the screen. Make sure you run it from a proper terminal/console and not in Eclipse.

    0 讨论(0)
  • 2020-11-22 00:09

    You can use an emulation of cls with for (int i = 0; i < 50; ++i) System.out.println();

    0 讨论(0)
  • 2020-11-22 00:11

    You can use following code to clear command line console:

    public static void clearScreen() {  
        System.out.print("\033[H\033[2J");  
        System.out.flush();  
    }  
    

    For further references visit: http://techno-terminal.blogspot.in/2014/12/clear-command-line-console-and-bold.html

    0 讨论(0)
  • 2020-11-22 00:11

    Create a method in your class like this: [as @Holger said here.]

    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) {}
    }
    

    This works for windows at least, I have not checked for Linux so far. If anyone checks it for Linux please let me know if it works (or not).

    As 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
    

    I will not recommend you to use this method.

    0 讨论(0)
  • 2020-11-22 00:12

    This will work if you are doing this in Bluej or any other similar software.

    System.out.print('\u000C');
    
    0 讨论(0)
  • 2020-11-22 00:13

    Try the following :

    System.out.print("\033\143");
    

    This will work fine in Linux environment

    0 讨论(0)
提交回复
热议问题