1

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~

Taryn
  • 242,637
  • 56
  • 362
  • 405
xialu
  • 174
  • 3
  • 11
  • http://www.vineetmanohar.com/2011/03/java-7-try-with-auto-closable-resources/ – Alon Aug 17 '15 at 03:49
  • 6
    If you take a look at the [JavaDocs for `BufferedReader#close`](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#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 – MadProgrammer Aug 17 '15 at 03:50
  • 3
    This question is under discussion on [meta](http://meta.stackoverflow.com/questions/319744/why-was-this-question-deleted). – George Stocker Mar 25 '16 at 17:56

2 Answers2

5

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.

Aequitas
  • 2,205
  • 1
  • 25
  • 51
  • 4
    Yes, but the question is, does the OP need to close the `InputStreamReader` themselves...`try (InputStreamReader is = new InputStreamReader(...); BufferedReader br = new BufferedReader (is)) {...}` – MadProgrammer Aug 17 '15 at 03:50
  • http://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html#close-- : Description copied from class: Reader Closes the stream and releases any system resources associated with it – Ravindra babu Aug 17 '15 at 04:00
  • 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. – xialu Aug 17 '15 at 08:13
1

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.

River
  • 8,585
  • 14
  • 54
  • 67