Forums Register Login

differents between PrintWriter and PrintStream

+Pie Number of slices to send: Send
Hello~ i got some puzzle about PrintWriter and PrintStream


what's the differents between those two methods, as i know PrintWiter mainly handle Unicode(char) ,PrintStream for byte.
Is that mean if i use
PrintStream method println(),print a chinese word such as "中国",when this code run in a USA computer it's will be translated into "China" automatic?? But the method of PrintWriter will give unreadable code?


thanks,
Xu ,
+Pie Number of slices to send: Send
The principal difference between using the ...Stream classes and the ...Reader/...Writer classes is that the former deal with raw bytes, while the latter deal with characters. Dealing with characters requires that specific "character encoding" be specified; Unicode in its various shapes (UTF-8, UTF-16, ...) is one such encoding.

If you looks at the javadocs for PrintStream.print you'll see that it uses the default character encoding to do this (which is most likely NOT Unicode). The same thing happens if you construct a Writer from a Stream (look at the javadocs for the PrintWriter(OutputStream) constructor).

Is that mean if i use PrintStream method println(),print a chinese word such as "中国",when this code run in a USA computer it's will be translated into "China" automatic?


I don't know what those two characters mean, but no automatic translation of any kind will happen. The encoding being used may be different, because different machines and operating systems have different default encodings.

This is an important point to keep in mind: If the platform default encoding is used, then the same code running on different machines may produce different output - while the text should be the same, the encoding may be different, and thus the files will not identical. Any code reading and writing text files needs to be aware of this issue, and make sure that its input and output is correctly encoded.
+Pie Number of slices to send: Send

thanks
now i think understand it.
We usually use the one for byte,so i can't distinguish the different .
Acutally 16bit is suitable when dealing with different OS isn't it?

And thanks a lot for reminder me that no translate will done automatic.

Happy~~
+Pie Number of slices to send: Send
Useful link that details about unicode and character sets.
+Pie Number of slices to send: Send
 

Acutally 16bit is suitable when dealing with different OS isn't it?


I'm not sure what you mean by "16 bit". All encodings -MacRoman, UTF-8, UTF-16, UTF-32, CP-1252, ISO-8859, ...- can be used cross-platform, as long as they are available on all JVMs being used, and as long as they are specified whenever bytes are converted to characters and vice versa.

This means that using the code you have shown above -which uses the platform default encoding- will NOT work for data being used on different platforms.
+Pie Number of slices to send: Send
 

Originally posted by Nitesh Kant:
Useful link that details about unicode and character sets.



That's a nice link Nitesh Thank you
+Pie Number of slices to send: Send
 

Originally posted by Raghavan Muthu:
That's a nice link


If you were unaware of that article, you might want to browse through the JavaRanch FAQs, especially the Java I/O FAQ for this particular case. Those pages link to lots and lots of highly useful articles. (Not to mention the many directly answered questions, of course.)
[ January 23, 2008: Message edited by: Ulf Dittmer ]
+Pie Number of slices to send: Send
 

Originally posted by Ulf Dittmer:

If you were unaware of that article, you might want to browse through the JavaRanch FAQs, especially the Java I/O FAQ for this particular case. Those pages link to lots and lots of highly useful articles. (Not to mention the many directly answered questions, of course.)



No Ulf. I was not aware of this.

Thank you for the links. of course i missed to see this. You are very true about this link
+Pie Number of slices to send: Send
Thanks for all firends~~

Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3226 times.
Similar Threads
why String vlass reference variable holds content ?
what is the difference between printStream and PrintWriter?
About PrintWriter with println and write
Casting - WHY does Runnable r1 = (Runnable)aa give NO compiler error
getting PrintStream or PrintWriter from logger
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:46:07.