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.
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.
So, if you don't close(), system resources may be still associated with the reader which may cause memory leak.
Therefore, if you close the Reader, you don't need to also close the InputStream.
You should always close your resources.
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
.
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)
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With