Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • 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

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic