• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Buffered Character Streams

 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
I'm working through some examples from the book "Teach Yourself Java" by O'Neil and am puzzled about something.
He is talking about buffered character streams.
Here is a snippet of code from his sample program "BufferedWriterDemo":
try
{
// create a file writer
FileWriter fw = new FileWriter(args[0]);
// create a buffered writer
BufferedWriter bw = new BufferedWriter(fw);
// write to file here ...
// close buffered writer
bw.close();
}
Here is a snippet of code from his ACCOMPANYING sample program "BufferedReaderDemo":
try
{
// create a file reader
FileReader fr = new FileReader(args[0]);
// create a buffered reader
BufferedReader = new BufferedReader(fr);
// read from file here ...
// close the file reader
fr.close();
}
Why in the first case do we close the buffered writer but NOT the file writer, and in the second case we close the file reader but NOT the buffered reader? This seems sloppy and inconsistent to me. What am I missing? Thanks in advance.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephanie Grasson:
Hi all.
I'm working through some examples from the book "Teach Yourself Java" by O'Neil and am puzzled about something.
He is talking about buffered character streams.
Here is a snippet of code from his sample program "BufferedWriterDemo":
try
{
// create a file writer
FileWriter fw = new FileWriter(args[0]);
// create a buffered writer
BufferedWriter bw = new BufferedWriter(fw);
// write to file here ...
// close buffered writer
bw.close();
}
Here is a snippet of code from his ACCOMPANYING sample program "BufferedReaderDemo":
try
{
// create a file reader
FileReader fr = new FileReader(args[0]);
// create a buffered reader
BufferedReader = new BufferedReader(fr);
// read from file here ...
// close the file reader
fr.close();
}
Why in the first case do we close the buffered writer but NOT the file writer, and in the second case we close the file reader but NOT the buffered reader? This seems sloppy and inconsistent to me. What am I missing? Thanks in advance.

In this example, you have only created one input or output stream. You have two refeneces to it, in the Input stream example they are FileReader and BufferedReader references but, there is still only one stream. When you close the stream using either reference, it's closed for both references. In a case such as this, you always treat it as if the highest reference is the only one available, after all, you created that reference to allow the mainipulation of the stream using the functionality of the BufferedReader.

[This message has been edited by Carl Trusiak (edited June 30, 2000).]
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl,
Thanks for the explanation! I didn't realize that only one stream object was created with multiple references to it, so that clears up a lot of things.
I'm still not clear, though, on how to decide which reference to use when closing the stream. You said that "In a case such as this, you always treat it as if the highest reference is the only one available". Why is a FileReader considered higher than a BufferedReader, but a BufferedWriter considered higher than a FileWriter? Could you (or anyone else) please explain? Thanks!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic