我可以找到控制台宽度与Java吗? [英] Can I find the console width with Java?

查看:320
本文介绍了我可以找到控制台宽度与Java吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法找到我的Java程序运行控制台的宽度?

Is there a way to find the width of the console in which my Java program is running?

如果可能,我希望这是跨平台...

I would like this to be cross platform if possible...

我不想改变缓冲区或窗口的宽度,我只想知道它的宽度,以便我可以正确地格式化正在打印到屏幕的文本。

I have no desire to change the width of the buffer or the window, I just want to know its width so I can properly format text that is being printed to screen.

推荐答案

这个问题没有可靠跨平台解决方案。实际上,存在不可能知道真实控制台宽度的情况。

There are no reliable cross-platform solutions to this problem. Indeed, there are situations where it is not possible to know what the real console width is.

例如,在Linux系统上,通常可以从LINES和COLUMNS环境变量中找出虚拟终端尺寸。虽然当您调整一些终端仿真器窗口时,这些变量会自动更新,但情况并非总是如此。事实上,在通过telnet协议连接的远程控制台的情况下,没有办法获得用户shell的实际终端尺寸。

For example, on a Linux system you can typically find out the notional terminal dimensions from the LINES and COLUMNS environment variables. While these variables are automatically updated when you resize some "terminal emulator" windows, this is not always the case. Indeed, in the case of a remote console connected via telnet protocol, there is no way to get the actual terminal dimensions to the user's shell.

EDIT :只是要补充说,如果用户在启动Java应用程序后在Linux上更改其xterm的大小,Java应用程序将不会收到通知,并且不会在其副本中看到新的维度LINES和COLUMNS环境变量!

EDIT: Just to add that if the user changes the dimensions of his/her xterm on Linux after launching a Java app, the Java app won't be notified, and it won't see the new dimensions reflected in its copy of the LINES and COLUMNS environment variables!

EDIT 2 :我的错误,他们是 bash shell变量,默认导出到环境。您可以在运行Java应用程序之前运行 export COLUMNS LINES 修复。

EDIT 2: My mistake, they are bash shell variables, but are not exported to the environment by default. You can "fix" this by running export COLUMNS LINES before you run your Java application.

这篇关于我可以找到控制台宽度与Java吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆