my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes differents between PrintWriter and PrintStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "differents between PrintWriter and PrintStream" Watch "differents between PrintWriter and PrintStream" New topic
Author

differents between PrintWriter and PrintStream

Xu Jun
Greenhorn

Joined: Dec 28, 2007
Posts: 12
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 ,


吾生也有涯,而学也无涯,以有涯随无涯,饴已~
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
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.


Ping & DNS - my free Android networking tools app
Xu Jun
Greenhorn

Joined: Dec 28, 2007
Posts: 12

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~~
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Useful link that details about unicode and character sets.


apigee, a better way to API!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
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.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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


That's a nice link Nitesh Thank you


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41123
    
  45
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 ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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
Xu Jun
Greenhorn

Joined: Dec 28, 2007
Posts: 12
Thanks for all firends~~

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: differents between PrintWriter and PrintStream
 
Similar Threads
About PrintWriter with println and write
getting PrintStream or PrintWriter from logger
why String vlass reference variable holds content ?
Casting - WHY does Runnable r1 = (Runnable)aa give NO compiler error
what is the difference between printStream and PrintWriter?