Problem with writing unicode characters in a file
I am trying to write some chinese characters into a file and according to my code, I get squares, question marks or other weird characters.
Basically, I just have a list of 4-digits Strings, representing the characters' unicodes and I want to transform them into a '\u####' that Java can interpret like a character (e.g. "4E0A" => \u4E0A)
Here is the code responsible to write in the file :
Of course, If I just replace ChineseChar by this :
[COLOR="DarkOrange"][B]// I did with PrintWriter before but didn't work so I changed to Writer[/B][/COLOR]
Writer writer = new OutputStreamWriter(new FileOutputStream(filename,false), "UTF-8");
sb = new StringBuilder();
[COLOR="DarkOrange"][B]// cs_temp.getUnicode() returns a String representing a 4-digits unicode (e.g. "4E00").
// If I just print chineseChar on a JLabel for instance, it works;
// and actually that's how I do to get a valid unicode from just a 4-digits String.[/B][/COLOR]
String chineseChar = sb.appendCodePoint(Integer.parseInt(cs_temp.getUnicode(),16)).toString();
writer.write(chineseChar , 0, strb.length());
It works, the two characters are correctly written in the file :
String chineseChar = "\u4E0A\u6D77";
So, why does it not work with a StringBuilder with the write() method ? I don't understand because it works well when I want to display them on any JComponent, but not when I try to write in a file :confused:
Any idea ?
Thanks in advance!