Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

differents between PrintWriter and PrintStream

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ,
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Xu Jun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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~~
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Useful link that details about unicode and character sets.
 
Ulf Dittmer
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



That's a nice link Nitesh Thank you
 
Ulf Dittmer
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all firends~~

 
Popeye has his spinach. I have this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic