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

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


   
   
  1. byte[] data = new byte[ 1024];
  2. InputStream in = null;
  3. OutputStream out = null;
  4. try {
  5. in = new FileInputStream( "foo.txt");
  6. in.read(data);
  7. out = new FileOutputStream( "foo.txt");
  8. data = "Hello, World".getBytes();
  9. out.write(data);
  10. IOUtils.copy( in, out);
  11. } catch (IOException e) {
  12. // error handling
  13. } finally {
  14. if ( in != null) {
  15. try {
  16. in.close();
  17. } catch (IOException e) {
  18. LOGGER.warn( "Fail to close stream : ", e);
  19. }
  20. }
  21. if ( out != null) {
  22. try {
  23. out.close();
  24. } catch (IOException e) {
  25. LOGGER.warn( "Fail to close stream : ", e);
  26. }
  27. }
  28. }

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


   
   
  1. byte[] data = new byte[ 1024];
  2. InputStream in = null;
  3. OutputStream out = null;
  4. try {
  5. in = new FileInputStream( "foo.txt");
  6. in.read(data);
  7. out = new FileOutputStream( "foo.txt");
  8. data = "Hello, World".getBytes();
  9. out.write(data);
  10. IOUtils.copy( in, out);
  11. in.close(); //close errors are handled
  12. out.close();
  13. } catch (IOException e) {
  14. // error handling
  15. } finally {
  16. IOUtils.closeQuietly( in);
  17. IOUtils.closeQuietly( out);
  18. }

closeQuietly 的内部实现如下:


   
   
  1. /**
  2. * Closes an <code>Reader</code> unconditionally.
  3. * <p>
  4. * Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
  5. * This is typically used in finally blocks.
  6. * <p>
  7. * Example code:
  8. * <pre>
  9. * char[] data = new char[1024];
  10. * Reader in = null;
  11. * try {
  12. * in = new FileReader("foo.txt");
  13. * in.read(data);
  14. * in.close(); //close errors are handled
  15. * } catch (Exception e) {
  16. * // error handling
  17. * } finally {
  18. * IOUtils.closeQuietly(in);
  19. * }
  20. * </pre>
  21. *
  22. * @param input the Reader to close, may be null or already closed
  23. */
  24. public static void closeQuietly(final Reader input) {
  25. closeQuietly((Closeable) input);
  26. }
  27. /**
  28. * Closes an <code>Writer</code> unconditionally.
  29. * <p>
  30. * Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
  31. * This is typically used in finally blocks.
  32. * <p>
  33. * Example code:
  34. * <pre>
  35. * Writer out = null;
  36. * try {
  37. * out = new StringWriter();
  38. * out.write("Hello World");
  39. * out.close(); //close errors are handled
  40. * } catch (Exception e) {
  41. * // error handling
  42. * } finally {
  43. * IOUtils.closeQuietly(out);
  44. * }
  45. * </pre>
  46. *
  47. * @param output the Writer to close, may be null or already closed
  48. */
  49. public static void closeQuietly(final Writer output) {
  50. closeQuietly((Closeable) output);
  51. }
  52. /**
  53. * Closes an <code>InputStream</code> unconditionally.
  54. * <p>
  55. * Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
  56. * This is typically used in finally blocks.
  57. * <p>
  58. * Example code:
  59. * <pre>
  60. * byte[] data = new byte[1024];
  61. * InputStream in = null;
  62. * try {
  63. * in = new FileInputStream("foo.txt");
  64. * in.read(data);
  65. * in.close(); //close errors are handled
  66. * } catch (Exception e) {
  67. * // error handling
  68. * } finally {
  69. * IOUtils.closeQuietly(in);
  70. * }
  71. * </pre>
  72. *
  73. * @param input the InputStream to close, may be null or already closed
  74. */
  75. public static void closeQuietly(final InputStream input) {
  76. closeQuietly((Closeable) input);
  77. }
  78. /**
  79. * Closes an <code>OutputStream</code> unconditionally.
  80. * <p>
  81. * Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
  82. * This is typically used in finally blocks.
  83. * <p>
  84. * Example code:
  85. * <pre>
  86. * byte[] data = "Hello, World".getBytes();
  87. *
  88. * OutputStream out = null;
  89. * try {
  90. * out = new FileOutputStream("foo.txt");
  91. * out.write(data);
  92. * out.close(); //close errors are handled
  93. * } catch (IOException e) {
  94. * // error handling
  95. * } finally {
  96. * IOUtils.closeQuietly(out);
  97. * }
  98. * </pre>
  99. *
  100. * @param output the OutputStream to close, may be null or already closed
  101. */
  102. public static void closeQuietly(final OutputStream output) {
  103. closeQuietly((Closeable) output);
  104. }
  105. /**
  106. * Closes a <code>Closeable</code> unconditionally.
  107. * <p>
  108. * Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in
  109. * finally blocks.
  110. * <p>
  111. * Example code:
  112. * </p>
  113. * <pre>
  114. * Closeable closeable = null;
  115. * try {
  116. * closeable = new FileReader(&quot;foo.txt&quot;);
  117. * // process closeable
  118. * closeable.close();
  119. * } catch (Exception e) {
  120. * // error handling
  121. * } finally {
  122. * IOUtils.closeQuietly(closeable);
  123. * }
  124. * </pre>
  125. * <p>
  126. * Closing all streams:
  127. * </p>
  128. * <pre>
  129. * try {
  130. * return IOUtils.copy(inputStream, outputStream);
  131. * } finally {
  132. * IOUtils.closeQuietly(inputStream);
  133. * IOUtils.closeQuietly(outputStream);
  134. * }
  135. * </pre>
  136. *
  137. * @param closeable the objects to close, may be null or already closed
  138. * @since 2.0
  139. */
  140. public static void closeQuietly(final Closeable closeable) {
  141. try {
  142. if (closeable != null) {
  143. closeable.close();
  144. }
  145. } catch ( final IOException ioe) {
  146. // ignore
  147. }
  148. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值