2

I have tried the following:

System.out.println("rājshāhi");
new PrintWriter(new OutputStreamWriter(System.out), true).println("rājshāhi");
new PrintWriter(new OutputStreamWriter(System.out, "UTF-8"), true).println("rājshāhi");
new PrintWriter(new OutputStreamWriter(System.out, "ISO-8859-1"), true).println("rājshāhi");

Which yields the following output:

r?jsh?hi
r?jsh?hi
rÄ?jshÄ?hi
r?jsh?hi

So, what am I doing wrong?

Thanks.

P.S.

I am using Eclipse Indigo on Windows 7. The output goes to the Eclipse output console.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
mark
  • 59,016
  • 79
  • 296
  • 580

5 Answers5

3

The java file must be encoded correctly. Look in the properties for that file, and set the encoding correctly:

enter image description here

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • The console output uses the encoding that is defined for the file. So, even if your program is correct, console output might show garbled characters. Don't go with console output. Try to write it to a file using proper encoding and check the result in any text viewer. Hope this helps – Ravi May 28 '12 at 05:30
2

What you did should work, even the simple System.out.println if you have a recent version of eclipse.

Look at the following:

  • The version of eclipse you are using
  • Whether the file is encoded correctly. See @Matthew's answer. I assume this would be the case because otherwise eclipse wouldn't allow you to save the file (would warn "unsupported characters")
  • The font for the console (Windows -> Preferences -> Fonts -> Default Console Font)
  • When you save the text to a file whether you get the characters correctly

enter image description here

Nivas
  • 18,126
  • 4
  • 62
  • 76
  • I'm pretty sure it has to do with the default encoding that the JVM uses is the system default. So the fact that it worked fine on yours likely has no direct relationship to why it's not working on the OPs. (See [this answer](http://stackoverflow.com/a/623036/971592)) – kentcdodds May 25 '12 at 14:04
  • @kentcdodds It may not be just the JVM default encoding (but definitely a possibility). I have had situations when the java class was not saved in a correctly encoded file. Thats why I asked OP to trysaving to a file (rather than printing to the console) – Nivas May 25 '12 at 17:53
1

Actually, copying your code and running it on my computer gave me the following output:

rājshāhi
rājshāhi
rājshāhi
r?jsh?hi

It looks like all lines work except the last one. Get your System default character set (see this answer). Mine is UTF-8. See if changing your default character set makes a difference.

Either of the following lines will get your default character set:

System.out.println(System.getProperty("file.encoding"));
System.out.println(Charset.defaultCharset());

To change the default encoding, see this answer.

Community
  • 1
  • 1
kentcdodds
  • 27,113
  • 32
  • 108
  • 187
0

Make sure when you are creating your class Assign the Text file Encoding Value UTF-8. Once a class is created with any other Text File Encoding later on you can't change the Encoding syle even though eclipse will allow you it won't reflect. So create a new class with TextFile Encoding UTF 8.It will work definitely.

EDIT: In your case though you are trying to assing Text File encoding programatically it is not making any impact it is taking the container inherited encoding (Cp1252)

BOSS
  • 2,931
  • 8
  • 28
  • 53
0

Using Latest Eclipse version helped me to achive UTF-8 encoding on console I used Luna Version of Eclipse and set Properties->Info->Others->UTF-8