java System.out 字符编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14030811/
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
System.out character encoding
提问by Lajos Arpad
I'm running my Java program from command-line (Windows 7). To simplify matters, I describe only the relevant part.
我正在从命令行(Windows 7)运行我的 Java 程序。为了简单起见,我只描述相关部分。
public static void main(String[] args) {
System.out.println("árpád");
}
My output is garbage. It is obviously a character-encoding problem, the Hungarian characters of á and á are not showing up correctly. I've tried the following:
我的输出是垃圾。很明显是字符编码问题,á 和 á 的匈牙利字符没有正确显示。我尝试了以下方法:
public static void main(String[] args) {
PrintStream ps = new PrintStream(System.out, true, "UTF-8");
ps.println("árpád");
}
But my output is still garbage. How can I resolve this character-encoding issue with Windows 7 command-line? Thanks
但我的输出仍然是垃圾。如何使用 Windows 7 命令行解决此字符编码问题?谢谢
回答by Jon Skeet
I got your code to work by finding the right encoding from the command line, and then eitherusing the PrintStream
version with that encoding, orby specifying it on the command line and just using System.out.println
.
我通过在命令行中找到正确的编码,然后有你的代码的工作要么使用PrintStream
,与编码版本或通过指定它的命令行上,只是利用System.out.println
。
To find the encoding on the commandline, run chcp
. Here's the output I got:
要在命令行上查找编码,请运行chcp
. 这是我得到的输出:
Active code page: 850
That corresponds to the Java charset name of "IBM850". So this then creates the right output on the command line:
这对应于“IBM850”的 Java 字符集名称。因此,这将在命令行上创建正确的输出:
java -Dfile.encoding=IBM850 Test