编码 cp1252

发布于 2024-08-12 14:14:57 字数 237 浏览 5 评论 0原文

当我在 Java 中尝试以下操作时:

System.out.println(System.getProperty("file.encoding"));

我得到 cp1252 作为编码。

有没有办法知道这个值从哪里来? (如环境变量或其他东西)

我想使用一些命令(如 Windows XP 上的 systeminfo)在命令提示符下打印编码值。

When I try the following in Java:

System.out.println(System.getProperty("file.encoding"));

I get cp1252 as the encoding.

Is there a way to know where this value is coming from? (Like Environment variables or something)

I would like to print the value of encoding on command prompt using some command like systeminfo on Windows XP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

美羊羊 2024-08-19 14:14:58

cp1252 是 MS Windows 英文版安装的默认编码(Microsoft 将其称为 ANSI)。 Java 默认情况下会将系统区域设置作为其默认字符编码。这意味着什么取决于系统。一般来说,我不喜欢依赖默认编码。如果我知道我的文本将是纯 ASCII,我会忽略它 - 否则我会在实例化 InputStreamReaderOutputStreamWriterString 等或调用时显式设置编码获取字节

请注意,cp1252 不是 Windows 命令提示符上的默认编码。这是更旧的 cp437,您可以使用 chcp 命令查看(和更改)它。

cp1252 is the default encoding on English installations of MS Windows (what Microsoft refers to as ANSI). Java by default will take the system locale as its default character encoding. What this means is system dependent. In general I don't like to rely on default encodings. If I know my text will be pure ASCII I ignore it - otherwise I set the encoding explicitly when instantiating InputStreamReader, OutputStreamWriter, String etc or calling getBytes.

Note that cp1252 is not the default encoding on the Windows command prompt. That is the even older cp437, which you can see (and change) using the chcp command.

聆听风音 2024-08-19 14:14:58

该值(至少在 Windows 上)是用于非 Unicode 文本的旧代码页。当您使用旧的 ANSI API 时,操作系统会将字符串转换为字符串。对于任何较新的程序,它应该没有任何效果(话虽如此,我仍然看到足够多的程序使用 API 函数的 A 变体而不是 W 变体,遗憾的是)。

对于 Java 程序来说,这些都不重要,因为 Java 只使用 Unicode。但是,如果您想在系统代码页中写入或读取文本文件,那么您将需要它。

然而,对于命令提示符来说,该编码没有重要价值,因为控制台默认使用模仿 DOS 时代之一的 OEM 编码(850 或 437 很常见)。

That value is, on Windows at least, the legacy codepage used for non-Unicode text. It's what the OS converts strings to and from when you use the old ANSI APIs. For any newer program it should have no effect (that being said, I still see enough programs that use the A and not the W variants of API functions, sadly).

For you Java program none of that should matter, as Java uses Unicode exclusively. If you want to write or read text files in the system's codepage, then you'll need it, however.

For the command prompt, however, that encoding is of no significant value, as the console by default uses the OEM encoding which mimics the one of the DOS ages (850 or 437 is pretty common).

年少掌心 2024-08-19 14:14:58

由于这与 Java 没有任何关系,因此您可以选择使用 WSH 脚本:

' save this script as printANSI.vbs
' usage: cscript /Nologo printANSI.vbs
Set objShell = CreateObject("WScript.Shell")
cp = objShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001" &_
                              "\Control\Nls\CodePage\ACP")
WScript.Echo cp

另请参阅chcp 命令;您可能想了解编码在 Windows 命令提示符上的工作原理 (这篇博文中的一些链接)。

Since this doesn't really have anything to do with Java, you could just opt to use a WSH script:

' save this script as printANSI.vbs
' usage: cscript /Nologo printANSI.vbs
Set objShell = CreateObject("WScript.Shell")
cp = objShell.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001" &_
                              "\Control\Nls\CodePage\ACP")
WScript.Echo cp

See also the chcp command; you may want to read up on how encoding works on the Windows command prompt (some links in this blog post).

要走干脆点 2024-08-19 14:14:58

据我发现,这是你的java源文件的编码,一旦你改变它的文本文件编码,你的输出就会改变。
在 Eclipse 上,从资源属性更改它(Alt+Enter 或右键单击该文件,转到资源)。
将文本文件编码从 cp1252 更改为其他编码,例如 UTF-8,哇...您的输出将不再是 cp1252。

As far as I have discovered, this is the encoding of your java source file, your output will change once you change its text file encoding.
On eclipse, change it from Resource property (Alt+Enter or Right click on that file, go to Resource).
Change text file encoding from cp1252 to something else, say UTF-8, woo... Your output won't be cp1252 any longer..

A君 2024-08-19 14:14:58

我相信这个编码是由 JVM 设置的,所以从外部检索它是没有意义的

I believe this encoding is set by the JVM so it wouldn't make sense to retrieve it from outside

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文