I'm using the answer on How to convert InputStream to virtual File which uses org.apache.commons.io.IOUtils
to copy the given InputStream
to a FileOutputStream
in order to create a File
.
Should I close the InputStream
given?
You do need to close the input Stream, because the stream returned by the method you mention is actually FileInputStream or some other subclass of InputStream that holds a handle for a file. If you do not close this stream you have resource leakage.
The operating system will only allow a single process to open a certain number of files, and if you don't close your input streams, it might forbid the JVM from opening any more.
The java. io. InputStream. close() method closes this stream and releases any system resources associated with the stream.
Applications can re-use buffers by using the underlying methods directly. This may improve performance for applications that need to do a lot of copying. Wherever possible, the methods in this class do not flush or close the stream.
It is a best practice to close InputStream. See this question.
org.apache.commons.io.IOUtils.copy
do not close streams. So you have to close.
See Javadoc
Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.
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