• 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

what is the difference between stream classes and reader,writer classes

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please can someone help me?

I want to know what is the difference between the stream classes and the Reader and Writer classes? In what scenarios are stream classes used and when are reader/writer classes used? Also the advantages and disadvantages of using them.

Thanks in advance

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reader and Writer are used for reading and writing text (strings) and are built on top of InputStream and OutputStream.

Readers and writers know how to decode / encode binary data from / to text using a character encoding.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply.

What I understand from this is that Stream classes can be used to read/write byte data which are in non-text format like zip,jpg,video etc and Reader/Writer classes can be used to read/write text/string data.

Am I thinking on the right track?

Thanks in advance
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moieen,

Yes, that's generally the right idea. But just to clarify a possible area of confusion: there are many different possible text formats (e.g. ASCII, ISO-8859-1, UTF-8, UTF-16, UCS-2), and Readers/Writers know how to convert all of these text formats to Java characters and strings. In contrast, Streams don't have this encoding/decoding logic built in, so they just return the raw input bytes. Hence you might say that Readers/Writers are "smarter" than Streams.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kelvin Lim:
But just to clarify a possible area of confusion: there are many different possible text formats (e.g. ASCII, ISO-8859-1, UTF-8, UTF-16, UCS-2), and Readers/Writers know how to convert all of these text formats to Java characters and strings.



While this is correct, I think a bit more elaboration about encodings is in order. Readers apply a particular encoding to an InputStream, but they don't know out of the box which encoding is the correct one for a given stream. The InputStreamReader class -which is used for turning an InputStream into a Reader- has two constructors: one takes just the InputStream, and one that takes the encoding in addition to the InputStream. If the encoding is not specified, the platform default encoding is used - which may well be the wrong one to use, especially for data that originates on a different platform.

E.g., a text file written on OS X will by default be encoded with MacRoman, while Windows uses (I think) Cp-1252, and Linux something else entirely. While all these are identical for ASCII data, any use of accented characters (like umlauts) will lead to problems.

The same applies to Writers and OutputStreams.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks all
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic