开发者

commands in java to clear the screen

What command in 开发者_如何学PythonJava will let you clear the console in a command-line application?


I think what the OP wants is to clear the screen and move the cursor to the home position. For that try:

        final String ANSI_CLS = "\u001b[2J";
        final String ANSI_HOME = "\u001b[H";
        System.out.print(ANSI_CLS + ANSI_HOME);
        System.out.flush();


It depends on your console but if it supports ANSI escape sequences, then try this..

final static String ESC = "\033[";
System.out.print(ESC + "2J"); 


Run this sample program: it demonstrates how to clear the console using an escape sequence and reposition the cursor to position X=1, Y=1.

I tested it on several Linux terminals. Don't know, if it works under Windows.
Perhaps you can tell me ;)

Read this article about escape sequences.

import java.io.*;

public class Main {

public static final char ESC = 27;

public static void main(String[] args)
        throws Exception {
    Console c = System.console();
    if (c == null) {
        System.err.println("no console");
        System.exit(1);
    }

    // clear screen only the first time
    c.writer().print(ESC + "[2J");
    c.flush();
    Thread.sleep(200);

    for (int i = 0; i < 100; ++i) {
        // reposition the cursor to 1|1
        c.writer().print(ESC + "[1;1H");
        c.flush();

        c.writer().println("hello " + i);
        c.flush();

        Thread.sleep(200);
    }
}

}


Clearing a screen generally requires sending special control sequences specific to the screen/terminal that your application is running under. Options:

  1. If you know you will always running under a specific terminal and can find the proper control sequences to clear the screen for that terminal, just output those sequences. If you tell us the screen, we may be able to tell you the sequence (its likely somewhat ANSI/VT100/VT220 -compatible).

  2. Externally ensure your app is always run in a desired terminal, e.g. a script to start your app starts the app in the desired terminal. Then output the necessary character sequence to clear the screen.

  3. Take control of the terminal by using a terminal emulation library, i.e. you app is now a windowing app that creates a terminal window screen for the user to use. You then control what terminal you are emulating and will know what control sequences are needed.

  4. Use a terminal library (e.g. like the historic curses library) that detects the terminal and provides an uniform interface to its features. See this question:

    What's a good Java, curses-like, library for terminal applications?

  5. Fake it by writing a bunch of lines to the screen.


clearing for bash that is working for me:

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


If none of the above solutions works for you( as in my case ), try this solution:

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

I'm using Windows 8 and this solution worked for me. Hope it does to you as well. :)


There is always the obvious (and kludgy)..

int numRowsInConsole = 60;
for (int ii=0; ii<numRowsInConsole; ii++) {
    // scroll down one line
    System.out.println("");
}


System.out.println("Hello!");

prints the specified string and then moves the cursor to the next line.

System.out.print("Hello!");

prints the specified string and leaves the cursor immediately after that string.

To solve the problem, identified above, of the cursor being on the second line of the console, use print instead of println.


I did this in BlueJ and it worked perfectly: Try System.out.print("\f");


To my knowledge Windows 10's Command Window cmd.exe does not natively support ANSI ESC sequences, despite the rumors. To have the:

final String ANSI_CLS = "\u001b[2J";
System.out.print(ANSI_CLS);

method work you need a command line emulator that supports ANSI, such as ConEmu64.


There are two very simple ways to solve this, the first is the brute force option:

for (int i=1; i<=10; i++)
    System.out.println("\n");

The problem with this however is that it only pseudo clears the screen, you can scroll up to see the data, but don't fear, there is another way!

System.out.println("\f");

Voila! That should do the trick, although your cursor will be situated on the second line of the console after the screen is cleared.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜