Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to correctly output a Chinese character?

Reply
Thread Tools

How to correctly output a Chinese character?

 
 
chad
Guest
Posts: n/a
 
      03-04-2004
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?
 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      03-04-2004
chad <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
 
Jon A. Cruz
Guest
Posts: n/a
 
      03-04-2004
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?

 
Reply With Quote
 
Alun Harford
Guest
Posts: n/a
 
      03-05-2004
"chad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chinese characters in IE6 now showing correctly Fister HTML 7 01-13-2008 07:30 PM
cant show the chinese text correctly Nick Java 1 10-15-2006 09:26 AM
Output Parameter not being returned correctly - DAAB in ASP.NET WAP Alec MacLean ASP .Net 8 09-18-2006 08:46 PM
Re: How do I translate a Chinese web site and add Chinese character set to IE..? =A0 - =A0 Taking these two questions one at a time... =A Patrick Dunford NZ Computing 3 04-28-2004 07:15 PM
Output / Debug window output bug? John Bentley ASP .Net 0 09-10-2003 07:38 AM



Advertisments