Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to explicity close all streams, if they're wrapped in a buffer via java?

private InputStream input;
private InputStreamReader inputReader;
private BufferedReader reader;

try {
    input = new InputStream();
    inputStreamReader = new InputStreamReader(inputStream);
    reader = new BufferedReader(inputStreamReader);
    // do I/O operations
} catch (IOException e) {
    Log.d("IOException", "The Data Could Not Be Read =/");
} finally {
    try {
        reader.close(); // now will this, by default, close all other streams? OR
      /*
       * input.close(); inputStream.close(); //is this necessary, along with
       * reader.close();
       */
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

I came across this question today, and am not sure whether they'll be closed, since it's wrapped, or if it is still necessary to close all streams independently.

like image 539
3alto Avatar asked Dec 03 '15 19:12

3alto


People also ask

Do we have to close the stream always Java?

Yes you you should close it and you can close it if you pass it into a submethod. If you pass it into a Reader though, if you call close on the Reader it will also close the stream.

What happens if you don't close Bufferedreader?

So, if you don't close(), system resources may be still associated with the reader which may cause memory leak.

Do we need to close Inputstreamreader?

Therefore, if you close the Reader, you don't need to also close the InputStream.

Should we close Bufferedreader?

You should always close your resources.


Video Answer


3 Answers

If any reader or stream decorates another reader/stream, then closing the outer one also closes the inner. This can be implied from the Javadoc of Closeable#close():

Closes this stream and releases any system resources associated with it.

That applies to the underlying resources, too.

If you're very curious you can dig into the sources of those classes, eg. in BufferedReader:

public void close() throws IOException {
    synchronized (lock) {
        if (in == null)
            return;
        try {
            in.close();
        } finally {
            in = null;
            cb = null;
        }
    }
}

where in is the underlying Reader.

like image 200
Adam Michalik Avatar answered Oct 24 '22 05:10

Adam Michalik


Yes, decorated streams are closed too.

InputStream in = new FileInputStream("c:\\myfile.txt");
InputStreamReader reader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(reader);

bufferedReader.close();

in.read(); // throws an IOException (no such file or directory)
reader.read(); // throws an IOException (Stream closed)
like image 5
andrucz Avatar answered Oct 24 '22 05:10

andrucz


From Java 7 you can use the try-with-resource block (and the fact that closing reader closes the others)

try (BufferedReader reader = new BufferedReader(
                              new InputStreamReader(new FileInputStream(file), "UTF-8"))) {

    // do I/O operations
} catch(IOException e) {
    Log.d("IOException", "The Data Could Not Be Read =/", e);
} 
like image 4
Peter Lawrey Avatar answered Oct 24 '22 05:10

Peter Lawrey