Move the print position of the command line interface in Java without using an external library

In C, I recalled that I can move an invisible carriage around the command line interface screen relative to the position of the line and character, which means that I can make the program print any text anywhere on the screen. Do we have such a team in Java?

For example, here is the pseudo code in C:

int main(){ printf("launching program\n"); moveTo(4,3); //move to line 4 at character index 3 on the screen. printf("AAA"); moveTo(3,0); //move to line 3 at character index 0 on the screen. printf("BBB"); moveTo(2,1); //move to line 2 at character index 1 on the screen. printf("CCC"); return 0; } 

The following output will be displayed on the command line interface:

 launching program CCC BBB AAA 

Do we have an equivalent method in Java without using any external or third-party library in this case?

+2
source share
2 answers

The ability to do this is a property of the terminal, not the language. So, in principle, if you are connected to a sufficiently capable terminal emulator, then yes, of course, this is possible.

The goal of a library such as ncurses is to abstract the details of the gore terminal-dependent cursor movement, etc. You do not need something like ncurses, you can always directly emit the appropriate codes for your target terminal.

By, "is there an equivalent method in Java," you mean, are there libraries that can also provide you with terminal-agnostic abstractions? Yes (see Other Answers). But nothing is going to turn every host system into a JVM for the VT100 emulator. For example, good luck on Windows. In this sense, 2D graphics in Java is more universal than a terminal environment!

+2
source

JCurses (Java port of the ncurses from C) is one of the features

+5
source

Source: https://habr.com/ru/post/924176/


All Articles