简体   繁体   English

如何检测Unix终端字符编码?

[英]How to detect Unix terminal character encoding?

Is there a way to detect the character encoding set in the terminal which is calling my Java program? 有没有一种方法可以检测正在调用Java程序的终端中设置的字符编码? In Windows I can call the "chcp" tool and parse the output. 在Windows中,我可以调用“ chcp”工具并解析输出。

But what about in Linux or Mac? 但是在Linux或Mac中呢?

Check the locale(1) man page 检查locale(1)手册页

You can change this by setting the LANG environment variable 您可以通过设置LANG环境变量来更改此设置

$ export LANG=en_US.iso88591

$ locale
LANG=en_US.iso88591
LC_CTYPE="en_US.iso88591"
LC_NUMERIC="en_US.iso88591"
LC_TIME="en_US.iso88591"
LC_COLLATE="en_US.iso88591"
LC_MONETARY="en_US.iso88591"
LC_MESSAGES="en_US.iso88591"
LC_PAPER="en_US.iso88591"
LC_NAME="en_US.iso88591"
LC_ADDRESS="en_US.iso88591"
LC_TELEPHONE="en_US.iso88591"
LC_MEASUREMENT="en_US.iso88591"

LC_IDENTIFICATION="en_US.iso88591" LC_IDENTIFICATION = “en_US.iso88591”

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. 在任何其他类似Unix的操作系统上也是如此。 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. 我完全无法想象这是一个常见的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM