Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

java - System.out character encoding

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...