简体   繁体   中英

Character Encoding in Java

In eclipse, I changed the default encoding to ISO-8859-1. Then I wrote this:

String str = "Русский язык ";
PrintStream ps = new PrintStream(System.out, true, "UTF-8");
ps.print(str);

It should print the String correctly, as I am specifying UTF-8 encoding. However, it is not printing.

ISO-8859-1字符编码仅支持0到255之间的字符,其他任何东西都可能变成'?'

If you save the source file (the .java file) as ISO-8859-1 than str will be encoded by javac using ISO-8859-1. Your problem does not lie in the creation of PrintStream : the str you are printing is wrong from the beginning.

Yes, it looks like the terminal that your are sending this output does not support this encoding.

If you are running Eclipse, you could set the encoding as follows:

  • In Run Configurations...->Common ->Encoding->Other
  • Select UTF-8

You are basically telling the PrintStream writer to expect the input characters to be UTF-8 encoded and to output it as UTF-8. There is no conversion. If you set your IDE to use ISO-8859-1 as character encoding for your file, which in turns contains the input string than you pipe ISO-8859-1 encoded characters into an UTF-8 expecting writer. So the writer treats the bytes receiving as UTF encoded characters which will result in data junk.

Either set your IDE to encode your source files in UTF-8 and check that your characters are correctly displayed and stored. Or tell your writer to treat them as ISO-8859-1, either way should do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM