IOUtils.closeQuietly:在finally中关闭流时不需要再catch一遍IOException

在使用 stream 的时,往往要 try catch IOException。eric教导我要把流的关闭放到 finally 中去写,并且在 close 之前要判断一下是否为 null。但是 stream.close() 也会 throw IOException,这就导致在 finally 中 也需要 try catch 一下,于是代码就很长。如下:

 byte[] data = new byte[1024];
 InputStream in = null;
 OutputStream out = null;
 
 try {
       in = new FileInputStream("foo.txt");
       in.read(data);
 
       out = new FileOutputStream("foo.txt");
       data = "Hello, World".getBytes();
       out.write(data);
 
       IOUtils.copy(in, out);
 
 } catch (IOException e) { 
       // error handling
 } finally {
       if (in != null) {
          try {
              in.close();
          } catch (IOException e) {
              LOGGER.warn("Fail to close stream : ", e);
          }
       }
       if (out != null) {
          try {
              out.close();
          } catch (IOException e) {
              LOGGER.warn("Fail to close stream : ", e);
          }
       }
 }       

这时候就可以使用 IOUtils.closeQuietly 来精简代码:

 byte[] data = new byte[1024];
 InputStream in = null;
 OutputStream out = null;
 
 try {
       in = new FileInputStream("foo.txt");
       in.read(data);
 
       out = new FileOutputStream("foo.txt");
       data = "Hello, World".getBytes();
       out.write(data);
 
       IOUtils.copy(in, out);
 
       in.close(); //close errors are handled
       out.close();
 } catch (IOException e) { 
       // error handling
 } finally {
       IOUtils.closeQuietly(in);
       IOUtils.closeQuietly(out);
 }

closeQuietly 的内部实现如下: 

 /**
  * Closes an <code>Reader</code> unconditionally.
  * <p>
  * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
  * This is typically used in finally blocks.
  * <p>
  * Example code:
  * <pre>
  *   char[] data = new char[1024];
  *   Reader in = null;
  *   try {
  *       in = new FileReader("foo.txt");
  *       in.read(data);
  *       in.close(); //close errors are handled
  *   } catch (Exception e) {
  *       // error handling
  *   } finally {
  *       IOUtils.closeQuietly(in);
  *   }
  * </pre>
  *
  * @param input the Reader to close, may be null or already closed
  */
 public static void closeQuietly(final Reader input) {
     closeQuietly((Closeable) input);
 }
 
 /**
  * Closes an <code>Writer</code> unconditionally.
  * <p>
  * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
  * This is typically used in finally blocks.
  * <p>
  * Example code:
  * <pre>
  *   Writer out = null;
  *   try {
  *       out = new StringWriter();
  *       out.write("Hello World");
  *       out.close(); //close errors are handled
  *   } catch (Exception e) {
  *       // error handling
  *   } finally {
  *       IOUtils.closeQuietly(out);
  *   }
  * </pre>
  *
  * @param output the Writer to close, may be null or already closed
  */
 public static void closeQuietly(final Writer output) {
     closeQuietly((Closeable) output);
 }
 
 /**
  * Closes an <code>InputStream</code> unconditionally.
  * <p>
  * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
  * This is typically used in finally blocks.
  * <p>
  * Example code:
  * <pre>
  *   byte[] data = new byte[1024];
  *   InputStream in = null;
  *   try {
  *       in = new FileInputStream("foo.txt");
  *       in.read(data);
  *       in.close(); //close errors are handled
  *   } catch (Exception e) {
  *       // error handling
  *   } finally {
  *       IOUtils.closeQuietly(in);
  *   }
  * </pre>
  *
  * @param input the InputStream to close, may be null or already closed
  */
 public static void closeQuietly(final InputStream input) {
     closeQuietly((Closeable) input);
 }
 
 /**
  * Closes an <code>OutputStream</code> unconditionally.
  * <p>
  * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
  * This is typically used in finally blocks.
  * <p>
  * Example code:
  * <pre>
  * byte[] data = "Hello, World".getBytes();
  *
  * OutputStream out = null;
  * try {
  *     out = new FileOutputStream("foo.txt");
  *     out.write(data);
  *     out.close(); //close errors are handled
  * } catch (IOException e) {
  *     // error handling
  * } finally {
  *     IOUtils.closeQuietly(out);
  * }
  * </pre>
  *
  * @param output the OutputStream to close, may be null or already closed
  */
 public static void closeQuietly(final OutputStream output) {
     closeQuietly((Closeable) output);
 }
 
 /**
  * Closes a <code>Closeable</code> unconditionally.
  * <p>
  * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in
  * finally blocks.
  * <p>
  * Example code:
  * </p>
  * <pre>
  * Closeable closeable = null;
  * try {
  *     closeable = new FileReader(&quot;foo.txt&quot;);
  *     // process closeable
  *     closeable.close();
  * } catch (Exception e) {
  *     // error handling
  * } finally {
  *     IOUtils.closeQuietly(closeable);
  * }
  * </pre>
  * <p>
  * Closing all streams:
  * </p>
  * <pre>
  * try {
  *     return IOUtils.copy(inputStream, outputStream);
  * } finally {
  *     IOUtils.closeQuietly(inputStream);
  *     IOUtils.closeQuietly(outputStream);
  * }
  * </pre>
  *
  * @param closeable the objects to close, may be null or already closed
  * @since 2.0
  */
 public static void closeQuietly(final Closeable closeable) {
     try {
         if (closeable != null) {
             closeable.close();
         }
     } catch (final IOException ioe) {
         // ignore
     }
 }


--------------------- 
原文:https://blog.csdn.net/huanghanqian/article/details/82500132 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值