F
F
frosty77777772015-03-28 18:20:28
Java
frosty7777777, 2015-03-28 18:20:28

How in Java to remove the last character or line printed on the terminal screen?

Is there such a possibility in principle?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pi314, 2015-03-28
@frosty7777777

There is a possibility, but it has nothing to do with Java. This can be done using the so-called. Escape sequences, if they are supported by the terminal (if the terminal is running in emulation mode, for example, VT-52 or VT-100). Most of the terminals in Linux work in this mode.
You can delete the last line printed with a carriage return character (when the cursor is at position 1 of the next line after the line is displayed), for example, like this:

System.out.println("Hello world"); //Print the line

System.out.print(String.format("\033[%dA",1)); // Move cursor up by 1 line
System.out.print("\033[2K"); // Erase the line

A complete list of codes can be found, for example, here .
upd: You
can do a lot more with Escape sequences: switch colors, position the cursor, "erase" parts of a line or the entire screen, etc. etc. For heavy use of ANSII, especially under Windows, Jansi may be useful .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question