How to correctly output a Chinese character?

C

chad

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?
 
J

Jon Skeet

chad said:
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.
 
A

Alun Harford

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

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top