3

We all know that it is a good habit to close all the StreamReaders we have defined at the end of code.

Now we can see that two Readers was defined as below. BufferedReader and InputStreamReader.The BufferedReader was closed, but we are unable to close the InputStreamReader.

JAVA code:

BufferedReader in = new BufferedReader(new InputStreamReader( connection.getInputStream())); if (in != null) { in.close(); } 

The problem is here, if the InputStreamReader in the parentheses should be closed? Will this kind of code bring some problem to the program? Please tell me , thank you~

3
  • vineetmanohar.com/2011/03/… Commented Aug 17, 2015 at 3:49
  • 6
    If you take a look at the JavaDocs for BufferedReader#close, you will see it says "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.", which basically means that calling close on BufferedReader will close any associated (child) streams Commented Aug 17, 2015 at 3:50
  • 3
    This question is under discussion on meta. Commented Mar 25, 2016 at 17:56

2 Answers 2

6

It's important to close any resource that you use.

in.close will close BufferedReader, which in turn closes the resources that it itself uses ie. the InputStreamReader.

So what you are doing will be closing both of them assuming no exception occurs before you call in.close.

To ensure that it is closed no matter what use try-with-resources it will automatically close it for you once your block is done or when an exception occurs.

You can do it like this:

try(BufferedReader in = new BufferedReader(new InputStreamReader( connection.getInputStream()))){/*code here*/} 

All the resources in the try() are guranteed to be closed.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but the question is, does the OP need to close the InputStreamReader themselves...try (InputStreamReader is = new InputStreamReader(...); BufferedReader br = new BufferedReader (is)) {...}
docs.oracle.com/javase/8/docs/api/java/io/… : Description copied from class: Reader Closes the stream and releases any system resources associated with it
Thank you for you help. After checking about it, I found that you have taught me a useful function in java 1.7. All the elements in parentheses the should implement java.lang.AutoCloseable.
2

There is no problem with doing this, in fact the InputStreamReader documentation from the Java API does it.

In this case, the InputStreamReader is being used by the BufferedReader, which means they will both close when the BufferedReader close() function is called as it: "Closes the stream and releases any system resources associated with it." As the InputStreamReader is also very clearly "associated" with the stream, it too will be closed.

When you build a BufferedReader using an InputStreamReader this way, they really are the same reader bundled together, not two different readers.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.