Ritesh Kaushik
Ritesh Kaushik

Reputation: 735

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.

Upvotes: 3

Views: 7515

Answers (4)

Tobias N. Sasse
Tobias N. Sasse

Reputation: 2547

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.

Upvotes: 0

Reimeus
Reimeus

Reputation: 159754

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

Upvotes: 0

Matteo
Matteo

Reputation: 14930

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.

Upvotes: 4

Peter Lawrey
Peter Lawrey

Reputation: 533432

The ISO-8859-1 character encoding only supports characters between 0 and 255, and anything else is likely to be turned into '?'

Upvotes: 4

Related Questions