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. Advertising

  2. chad

    Jon Skeet Guest

    chad <> wrote:
    > 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.


    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 - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Mar 4, 2004
    #2
    1. Advertising

  3. chad

    Jon A. Cruz Guest

    chad wrote:

    > System.out.print(c); // no problem, the character diplays well.


    Don't believe it.
    :)


    > osw.write(c); // problem: writes an ? instead of that chinese char.


    Are you sure?

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

    Alun Harford Guest

    "chad" <> wrote in message
    news:...
    > 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.

    Output uses UNICODE so this works fine

    > osw.write(c); // problem: writes an ? instead of that chinese char.

    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U3BpZGVyX0ppYQ==?=

    how to diaplay chinese character in aspx page

    =?Utf-8?B?U3BpZGVyX0ppYQ==?=, May 27, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    775
    Natty Gur
    May 28, 2004
  2. Jeff
    Replies:
    3
    Views:
    824
    chris
    Jan 16, 2004
  3. Jeff
    Replies:
    3
    Views:
    806
    Jon A. Cruz
    Jan 17, 2004
  4. Nick
    Replies:
    1
    Views:
    332
    Lothar Kimmeringer
    Oct 15, 2006
  5. Fister
    Replies:
    7
    Views:
    4,184
    Bergamot
    Jan 13, 2008
Loading...

Share This Page