I'm trying to create a custom print stream that can print localized messages to the console. I encountered a problem doing this on Windows. Here is what I'm attempting to do
- I have a unicode string
- Convert unicode string to bytes using UTF-8 encoding
- Convert bytes to a new string with console encoding
- Print new string to console with console encoding
In this code, I tried to do the above steps but it fails miserably. Strangely the default System.out.println call works correctly. However, I want to use a custom print stream and not rely on the default System.out.
Can someone explain how I can print unicode to the console using my custom print stream? And why is the default System.out already equipped to print things correctly?
Here is my code - I compiled it and ran it from the command line. I set my system locale to zh-CN beforehand.
public static void main(String[] args) throws Exception{
Charset defaultCharset = Charset.defaultCharset();
System.out.println(defaultCharset);
// charset is windows-1252
String unicodeMessage =
"\u4e16\u754c\u4f60\u597d\uff01";
System.out.println(unicodeMessage);
// string is printed correctly using System.out (世界你好!)
byte[] sourceBytes = unicodeMessage.getBytes("UTF-8");
String data = new String(sourceBytes , defaultCharset.name());
PrintStream out = new PrintStream(System.out, true, defaultCharset.name());
out.println(data);
// prints gibberish: ??–????????????
}