在不使用外部库的情况下移动Java中命令行界面的打印位置
在 C 中,我记得我可以在命令行界面屏幕上相对于行和字符位置移动不可见的插入符号,这意味着我可以让程序在屏幕上的任何位置打印任何文本。 Java中有这样的命令吗?
例如,下面是 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;
}
这将在命令行界面中给出以下输出:
launching program
CCC
BBB
AAA
在这种情况下,我们在 Java 中是否有等效的方法而不使用任何外部或第三方库?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JCurses(来自 C 的
ncurses
库的 Java 端口)是一种可能性JCurses (Java port of
ncurses
library from C) is one possibility执行此操作的能力是终端的属性,而不是语言的属性。所以原则上,如果您连接到一个足够强大的终端仿真器,那么是的,当然这是可能的。
像 ncurses 这样的库的目的是抽象出依赖于终端的光标移动等的血腥细节。您不需要像 ncurses 这样的东西,您总是可以直接为您的程序发出适当的代码目标终端。
“Java 中是否有等效的方法”,您的意思是是否存在可以为您提供与终端无关的抽象的库?是的(参见其他回复)。但没有什么能让每个主机系统都向 JVM 提供一个 VT100 模拟器。例如,祝 Windows 好运。从这个意义上来说,Java中的2D图形比终端环境更通用!
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!