The moose likes I/O and Streams and the fly likes Streams Vs Reader/Writer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Streams Vs Reader/Writer" Watch "Streams Vs Reader/Writer" New topic

Streams Vs Reader/Writer

Balaji Sadasivam

Joined: Nov 16, 2000
Posts: 25

Streams (Input/Output) can be chained with Readers or Writers ???
The following example is from RHE.
1.FileOutputStream fos = new FileOutputStream("datafile");
2.DataOutputStream dos = new DataOutputStream(fos);
3.for (int i=0;i<500;i++)
4. dos.writeInt(i);
You would like to write code to read back the data from this file. Which solutions listed below will work? (Chose none, some, or all.)

Five possible answers are given and the following is one among them which is INCORRECT.
"Construct a FileReader, passing the name of the file. Onto the FileReader, chain a DataInputStream, and call its readInt() method."
The reason given in the book is "Streams can't be chained with Readers/Writers."
Look at the following piece of code, in which Stream is chained with Reader. (This is from Khalid Mughal)
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, "8859_1");
I think, I comprehend the concepts of Byte streams and Char streams partially.
can someone explain in detail.


Nasir Khan
Ranch Hand

Joined: Nov 04, 2000
Posts: 135
Hi Balaji ,
"Streams can't be chained with Readers/Writers." is a correct statement .If you go through all constructors of
Input/outputStream classes they never require a Reader or Writer Object as their arguments
The example you've given from Khalid Mughal's book tells us a different Story .The only classes InputStreamReader and OutputStreamWiter (in Reader \Writer heirarchy) can accept Inputstream and OutputStream object respectively as their
Correct me if I'm wrong....

Michael Burke
Ranch Hand

Joined: Jul 29, 2000
Posts: 103
InputStreamReader and OutputStreamWriter act as a bridge between char and byte streams.
Balaji Sadasivam

Joined: Nov 16, 2000
Posts: 25
Thanks guys.

Byte and Char streams are clear now.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I am moving this thread to I/O and Streams forum.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
I agree. Here's the link:
subject: Streams Vs Reader/Writer
It's not a secret anymore!