2

Case 1:

BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file) ) );
br.close();

Case 2:

BufferedReader br = new BufferedReader( new FileReader(file) );
br.close();

Case 3:

InputStream bis = new BufferedInputStream( new FileInputStream(src) );
bis.close();

What does close() do?

The docs say: "Closes the stream and releases any system resources associated with it."

Does it close any "underlying" resources also? Or would the correct code be to instantiate all the buffers and streams and close one by one?

tshepang
12.5k25 gold badges98 silver badges140 bronze badges
asked Feb 12, 2011 at 3:37

4 Answers 4

2

Yes, the underlying resources are released. The Java IO stream classes are a good example of the Decorator pattern, i.e. an object that has an interface identical to an object it contains. This allows the close() method to be very simple and elegant, e.g. something like:

public void close() {
 in.close();
}

If in is a decorator itself, its close method will close another resource, recursively.

If you're interested, java.io source code can be viewed online here.

deinocheirus
1,8635 gold badges26 silver badges44 bronze badges
answered Feb 12, 2011 at 4:08
Sign up to request clarification or add additional context in comments.

Comments

1

Does it close any "underlying" resources also?

Yes

Or the correct code would be to instantiate all the buffers and streams and close one by one?

No, you don't need to do that.

I've always considered it a docs deficiency that this isn't explicitly stated. It is however implied by the words "any system resources associated with it". Since, even with an extra layer in the middle, your BufferedReader is still associated with a file, to conform to the doc it needs to recurse into its wrapped streams. Once those resources have been been released it's just a job for the garbage collector once your stream stops being referenced.

answered Feb 12, 2011 at 3:40

Comments

0

you only need to close the outer most stream class because the close() call is automatically trickled through all the chained classes.

public void close() throws IOException Description copied from class: Reader

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

http://download.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#close%28%29

answered Feb 12, 2011 at 3:41

1 Comment

He's actually got the relevant part of that quote in his question.
0

Generally calls to close() will be cascaded down through underlying objects.

answered Feb 12, 2011 at 3:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.