aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference between stream classes and reader,writer classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the difference between stream classes and reader,writer classes" Watch "what is the difference between stream classes and reader,writer classes" New topic
Author

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

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
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

Joined: Aug 16, 2005
Posts: 14276
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
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

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
Ulf Dittmer
Marshal

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


Ping & DNS - my free Android networking tools app
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
thanks all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what is the difference between stream classes and reader,writer classes