System.out 字符编码 [英] System.out character encoding
问题描述
我正在从命令行 (Windows 7) 运行我的 Java 程序.为简单起见,我只描述相关部分.
I'm running my Java program from command-line (Windows 7). To simplify matters, I describe only the relevant part.
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");
}
但我的输出仍然是垃圾.如何使用 Windows 7 命令行解决此字符编码问题?谢谢
But my output is still garbage. How can I resolve this character-encoding issue with Windows 7 command-line? Thanks
推荐答案
我通过从命令行找到正确的编码,然后使用 PrintStream<任一 来让您的代码工作/code> 使用该编码的版本,或通过在命令行中指定它并仅使用
System.out.println
.
I got your code to work by finding the right encoding from the command line, and then either using the PrintStream
version with that encoding, or by specifying it on the command line and just using System.out.println
.
要在命令行上查找编码,请运行 chcp
.这是我得到的输出:
To find the encoding on the commandline, run chcp
. Here's the output I got:
Active code page: 850
这对应于IBM850"的 Java 字符集名称.因此,这将在命令行上创建正确的输出:
That corresponds to the Java charset name of "IBM850". So this then creates the right output on the command line:
java -Dfile.encoding=IBM850 Test
这篇关于System.out 字符编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!