In the stream tutorial, nothing is said about closing streams gained from Files.newInputStream( path )
. Only some obscure:
Whether the returned stream is asynchronously closeable and/or interruptible is highly file system provider specific and therefore not specified.
What is "asyncronously" in this context? If I close the stream explicitly or if another thread closes the stream asyncronously?
You definitely must close the obtained InputStream
, just like all others. The term "asynchronously closeable" refers to the ability to close the stream while another thread is blocked on an I/O operation on it.
From InterruptibleChannel
documentation:
A channel that implements this interface is asynchronously closeable: If a thread is blocked in an I/O operation on an interruptible channel then another thread may invoke the channel's close method. This will cause the blocked thread to receive an AsynchronousCloseException.
You can do this conveniently with the new try with resources option.
try(/*initialize resources here*/)
{
}
They will automatically be closed after the try block. Add a catch as necessary.
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