How to correctly output a Chinese character?

Discussion in 'Java' started by chad, Mar 4, 2004.

  1. chad

    chad Guest

    I attempted to output a Chinese character like below, but was not
    successful. I got a question mark (?) in place of that character.

    // please note that c is a chinese character.

    FileOutputStream fos = new FileOutputStream("file1.txt");
    OutputStreamWriter osw = new OutputStreamWriter(fos, "GB2312");
    System.out.print(c); // no problem, the character diplays well.
    osw.write(c); // problem: writes an ? instead of that chinese char.

    Any guru please?
     
    chad, Mar 4, 2004
    #1
    1. Advertisements

  2. chad

    Jon Skeet Guest

    How are you viewing the file you've written? Chances are you've not
    told it that the file is encoded in GB2312.
     
    Jon Skeet, Mar 4, 2004
    #2
    1. Advertisements

  3. chad

    Jon A. Cruz Guest

    Don't believe it.
    :)

    Are you sure?

    Did you examine the file in a hex editor?
     
    Jon A. Cruz, Mar 4, 2004
    #3
  4. chad

    Alun Harford Guest

    Output uses UNICODE so this works fine
    osw is a byte stream (in GB2312 character set)

    Try changing to UTF-16.

    Change to:
    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-16");

    Now you're writing in a charactor set that contains the characters you need.

    Alun Harford

    DISCLAIMER: I'm just learning Java so this may be total rubbish. Worse
    things happen. Get over it.
     
    Alun Harford, Mar 5, 2004
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.