This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Streams Vs Reader/Writer

 
Balaji Sadasivam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
Thanks

Regards
Balaji

 
Nasir Khan
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
arguments.
Correct me if I'm wrong....

 
Michael Burke
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
InputStreamReader and OutputStreamWriter act as a bridge between char and byte streams.
 
Balaji Sadasivam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.

Byte and Char streams are clear now.
Balaji
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this thread to I/O and Streams forum.

Ajith
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!