如何检测Unix终端字符编码?
有没有办法检测调用我的 Java 程序的终端中的字符编码集?在 Windows 中,我可以调用“chcp”工具并解析输出。
但在 Linux 或 Mac 上呢?
Is there a way to detect the character encoding set in the terminal which is calling my Java program? In Windows I can call the "chcp" tool and parse the output.
But what about in Linux or Mac?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 locale(1) 手册页
您可以通过设置 LANG 环境变量来更改它
LC_IDENTIFICATION="en_US.iso88591"
Check the locale(1) man page
You can change this by setting the LANG environment variable
LC_IDENTIFICATION="en_US.iso88591"
从终端应用程序到终端窗口内运行的进程没有定义的通信路径(缺少被模拟的终端定义的通信路径,例如窗口大小更改)。在任何其他类 Unix 操作系统上也是如此。环境变量只能在进程启动时继承。您所能做的就是查看这些环境变量的值。
您真的希望人们在终端窗口打开时更改编码吗?我根本无法想象这是一个常见的用例。
There is no defined communication path from the terminal app to processes running inside a terminal window (short of those defined by the terminal being emulated, such as window size changes). This would be true on any other Unix-like OS as well. Environment variables can only be inherited when a process starts. Looking at the values of these environment variables is all you can do.
Do you really expect people to be changing the encoding while a terminal window is open? I can't imagine this is a common use case at all.