Java 编码cp1252
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1826771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Encoding cp1252
提问by Arun
When I try the following in Java:
当我在 Java 中尝试以下操作时:
System.out.println(System.getProperty("file.encoding"));
System.out.println(System.getProperty("file.encoding"));
I get cp1252
as the encoding.
我得到cp1252
作为编码。
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.
我想在 Windows XP 上使用诸如 systeminfo 之类的命令在命令提示符下打印编码的值。
回答by tinkertime
I believe this encoding is set by the JVM so it wouldn't make sense to retrieve it from outside
我相信这种编码是由 JVM 设置的,因此从外部检索它是没有意义的
回答by Joey
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 shouldhave no effect (that being said, I still see enough programs that use the A and not the W variants of API functions, sadly).
该值至少在 Windows 上是用于非 Unicode 文本的旧代码页。当您使用旧的 ANSI API 时,这是操作系统在字符串之间进行转换的内容。对于任何较新的程序,它应该没有任何影响(也就是说,我仍然看到足够多的程序使用 API 函数的 A 而不是 W 变体,遗憾的是)。
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.
对于您的 Java 程序,这些都不重要,因为 Java 只使用 Unicode。但是,如果您想在系统的代码页中写入或读取文本文件,那么您将需要它。
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).
然而,对于命令提示符,该编码没有重要价值,因为控制台默认使用模仿 DOS 时代之一的 OEM 编码(850 或 437 很常见)。
回答by Dan
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
.
cp1252 是 MS Windows 英文安装的默认编码(微软称之为 ANSI)。默认情况下,Java 将系统区域设置作为其默认字符编码。这意味着什么取决于系统。一般来说,我不喜欢依赖默认编码。如果我知道我的文字将是纯ASCII我忽略它-否则我实例时设置的编码明确InputStreamReader
,OutputStreamWriter
,String
等或调用getBytes
。
Note that cp1252 is notthe default encoding on the Windows command prompt. That is the even older cp437, which you can see (and change) using the chcp
command.
请注意,cp1252不是Windows 命令提示符下的默认编码。那是更旧的 cp437,您可以使用chcp
命令查看(和更改)它。
回答by McDowell
Since this doesn't really have anything to do with Java, you could just opt to use a WSHscript:
由于这与 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
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).
另见chcp
命令;您可能想了解编码如何在 Windows 命令提示符下工作(此博客文章中的一些链接)。
回答by Appy
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..
据我所知,这是你的java源文件的编码,一旦你改变了它的文本文件编码,你的输出就会改变。在 Eclipse 上,从 Resource 属性更改它(Alt+Enter 或右键单击该文件,转到 Resource)。将文本文件编码从 cp1252 更改为其他内容,例如 UTF-8,哇...您的输出将不再是 cp1252..