aspose file tools*
The moose likes Java in General and the fly likes Buffered Character Streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Buffered Character Streams" Watch "Buffered Character Streams" New topic
Author

Buffered Character Streams

Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
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

Joined: Jun 13, 2000
Posts: 3340
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).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Buffered Character Streams