Should I use the close or closeQuietly method to close the output stream?

When we need to close the output stream, we have two options.

  • closeQuietly means closing the stream without exception.

    try { close(out) } catch(IOException e) { } 
  • close

     try { close(out) } catch(IOException e) { throw anException; } 

as you know, the output stream will write / several characters to the end of the file when closing, if these records go wrong, the file also cannot be opened correctly, for example ZipoutputStream.

If I use the first, I will get some risk of breaking the closure. if I use the second, it will allow my code to be unfriendly.

Can someone give me some advice?

Sorry for describing the issue unclear.

I meant how to safely get an I / O operation. if the release of the resource did not work, it will inform the caller about it.

Thanks for your reply. And especially thanks to @Don Roby for giving me a link that contains the best answer posted by @Fabian Barney

+7
source share
4 answers

Since Java 7 IOUtils.closeQuietly deprecated and the only sensible solution is try-with-resources , which automatically closes resources

 try (InputStream is = new FileInputStream(file)) { ... } 

Please note that it also solves the problem of correctly opening / closing more than one resource

 try (InputStream is = new FileInputStream(infile); OutputStream out = new FileOutputStream(outfile)) { ... } 

And it also does not suppress the IOException, which close() can closeQuietly , which is what closeQuietly does.

+11
source

Some implementations of close() may include other logic, such as writing trailing bytes or flush() 'data. Example: FilterOutputStream .

Now create a situation where the stream is based on a network channel or an external USB drive. Both can disappear at any time. This can happen when close () is executed.

So my opinion is: catch an IOException and throw an exception for a specific application with exception-exception enabled, for example:

 } catch (IOException e) { throw new IOManagementException(e); } 

If you are committed to not throwing an exception, then write down if with ERROR status at least.

If this is not done, it can lead to a very complicated analysis of error reports or strange behavior.

+3
source

As a rule, I never swallow exceptions, so depending on the logic required by the code I write, I might

  • Record the exception:

     try { close(out); } catch(IOException e) { // log the exception log.info("An error has occurred during stream closing: {}", e); } 
  • Wrap it further

     try { close(out); } catch(IOException e) { throw new MyException(e); } 

Since JDK 7 is around, I prefer (as Eugene mentioned) to use try-with-resources:

 try (OutputStream out = // create output stream) { // do the writing } // at this point the stream is closed 

This will be an appropriate and safe closure of the thread you are dealing with.

0
source

Pure code is good, but whenever it is compromised, it can give you better results, such as maintenance, Debugging in this case, we must do it. This can be done better, as a custom exception, or use the Java7 function as suggested above, but the cost of writing a few lines is worth what I think.

0
source

All Articles