• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I/O Insanity!!!!!!

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay ... I am starting to lose my mind over all these I/O Java classes and their differences. PrintStream is a high level OutputStream, and PrintWriter is a high level OutputWRITER. PrintStream being a subclass of the OutputStream writes bytes rather than chars to a file, right? But if this is so, then how is it that files created by PrintStreams are human readable (i.e. they are text files)?
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To keep your sanity, remember that, in the end, everything is just 1's or 0's!
"text" streams have been optimized to write data that has been created using the format that the host machine considers text. So PrintWriter has methods and is also designed to be more effecient with "text" data.
And PrintStream has methods and is designed to be more efficient with non "text" data, ie. it writes in byte-sized blocks. However, a Java char is made of two byte-sized blocks.
Two things to keep in mind when you are looking at these two stream objects:
1 - The data you are telling Java to print out is held in containers that represent human readable text in binary format that is governed by the unicode specification. The unicode specification represents the english alphabet in 0 through 127 decimal. Therefore, as Java promotes and chops the data up to prepare it to put on the PrintStream stream, the decimal value remains unchanged and when your text editor reads the data it sees those decimal values and displays the appropriate english character.
2 - PrintStream extends OutputStream, so even though the data is written out in byte sized chunks, if you are using the print or println methods, they have been overloaded to include a whole range of data types and your human readable data is being written byte by byte without losing its value. A decimal 65 stored in a byte is still decimal 65. A decimal 65 stored in an int is still 65. And a decimal 65 interpretted according to the ascii specification is an A.
[This message has been edited by Guy Reynolds (edited July 18, 2001).]
 
Dan Temple
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response! Well as you can probably gather, I know next to zilch when it comes to I/O, but if I understand you correctly then the PrintStream is limited to writing characters of Ascii (since it is only writing blocks of bytes). PrintWriter is writing characters in blocks of 2 bytes for the unicode characters. Hence the PrintStream is unable to write various encodings for unicode, but the PrintWriter is able to do this. Am I right so far?
But I am still a little perplexed about some other points -
1) PrintStream is supposed to write files in a human "readable" output. It does this by writing out everything in blocks of single bytes, so it can be easily read by a text editor. Am I correct here? But if this is the case then how exactly does this differ from a FileOutputStream which also writes out blocks of bytes to files, and yet a FileOutputStream writes binary files while a PrintStream writes out text files. From whence does this difference spring?
2) Other than the obvious difference (one is human readable, and the other isn't) what exactly IS the difference between a binary file and a text file?

3) Why did Coca-Cola try and change the taste of its main product back in the 80's when no one liked it?
Dan
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3) Because humans make stupid decisions. Corporations are simply aggregations of humans. Stupidity geometrically increases with the addition of humans.

Maybe I'm being a bit snippy.

Bill Cosby... how much were you paid to say "i like new Coke better".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic