what is the difference between stream classes and reader,writer classes
Moieen Khatri
Ranch Hand
Joined: Nov 27, 2007
Posts: 144
posted
0
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.
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
posted
0
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: 35232
7
posted
0
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.