Results 1 to 2 of 2
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Rep Power

    Default Problem with writing unicode characters in a file

    Hi everyone,

    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 :

    Java Code:
    [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());
    Of course, If I just replace ChineseChar by this :

    Java Code:
    String chineseChar = "\u4E0A\u6D77";
    It works, the two characters are correctly written in the file :

    Java Code:
    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!

  2. #2
    ze snow is offline Member
    Join Date
    Feb 2010
    Rep Power

Similar Threads

  1. Replies: 2
    Last Post: 02-15-2010, 06:49 AM
  2. Form Based Authentication fails with unicode characters
    By davesquire in forum Java Servlet
    Replies: 1
    Last Post: 09-25-2009, 06:00 PM
  3. Writing to file problem
    By PeraPisar in forum Advanced Java
    Replies: 12
    Last Post: 09-16-2009, 11:00 AM
  4. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 PM
  5. writing and reading unicode characters from a file
    By ranoosh in forum Advanced Java
    Replies: 4
    Last Post: 09-28-2008, 04:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts