user147886
user147886

Reputation: 41

Java - Control console output in windows/linux

Does Java support controlling the cursor when outputting to a console? For example, I'd like to set the character position, and possibly color, before doing a System.out.print(). Think of the way an application like top writes to the console. Thanks!

Upvotes: 4

Views: 3046

Answers (3)

Nir Levy
Nir Levy

Reputation: 4740

You usually do not use system.out to do these things. most applications in *nix use NCURSES (http://en.wikipedia.org/wiki/Ncurses) for this. You can try http://sourceforge.net/projects/javacurses/ if you need something this smart.

However, you can always sysout backspace (\b) characters if you want to delete what you wanted, and hope for the best

Upvotes: 3

Joshua
Joshua

Reputation: 1235

Ha. You can still do it in Linux. Reference this man page for the codes themselves http://man7.org/linux/man-pages/man4/console_codes.4.html

public class quickTest{
    public static void main( String[] args ){
            //This will undo the current line by erasing it
            //and then putting the curser back at column 1
        System.out.println( "Hello.\u001b[1K\u001b[1GHi." ); 
    }
}

Upvotes: 1

Not directly. In the old days ANSI escape sequences was supported, but not anymore.

I would suggest you look into a good Java Curses library supporting Windows. I cannot recommend any :(

Upvotes: 0

Related Questions