Move printing position of Command Line Interface in Java without using External library

╄→尐↘猪︶ㄣ 提交于 2020-01-23 12:33:39

问题


In C, I recalled that I can move the invisible caret around the command line interface screen with respect to the line and character position, meaning I can make the program print any text anywhere on the screen. Do we have such command in Java?

For instance, here is a pseudocode 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;
}

This will give the following output in 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?


回答1:


The ability to do this is a property of the terminal, not the language. So in principal, if you're connected to a sufficiently capable terminal emulator, then yes, of course it's possible.

The purpose of a library like ncurses is to abstract away the gory details of terminal-dependent cursor movement, etc. You don't need something like ncurses, you could always just directly emit the appropriate codes for your target terminal.

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




回答2:


JCurses (Java port of ncurses library from C) is one possibility



来源:https://stackoverflow.com/questions/4906141/move-printing-position-of-command-line-interface-in-java-without-using-external

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!