System.out character encoding

拈花ヽ惹草 提交于 2019-12-18 19:11:33

问题


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");
}

But my output is still garbage. How can I resolve this character-encoding issue with Windows 7 command-line? Thanks


回答1:


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.

To find the encoding on the commandline, run chcp. Here's the output I got:

Active code page: 850

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


来源:https://stackoverflow.com/questions/14030811/system-out-character-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!