I was wondering that what is the best/appropriate way to release file resources/handles.
Traditional code,
BufferredInputStream stream = null
try{
----
stream = new BufferredInputStream(new FileInputStream());
----
} finally{
if(stream != null){
stream.close()
}
}
Will the file handle be released by closing BufferredInputStream.close
alone or it needs the underlying stream(i.e. FileInputStream.close())
also to be called explicitly.
P.S. Javadoc for [FilterOutputStream.close]
method specifies that it will explicitly close the underlying stream too. But other streams doesn't seem to have this in the doc.
[FilterOutputStream.close]: http://docs.oracle.com/javase/1.4.2/docs/api/java/io/FilterOutputStream.html#close%28%29
Please advice. Thanks in advance.
You can always check the source code for the underlying class to determine the exact behavior.
However, in this case calling close()
on BufferedInputStream
will also close the underlying stream i.e. FileInputStream
.
The source code is available here
Your approach is correct. When in doubt, always check the source code. http://www.docjar.com/html/api/java/io/BufferedInputStream.java.html the close method is closing "in" which was chained to BufferedInputStream.
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