在使用 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("foo.txt"); * // 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 } }
Original url: Access
Created at: 2019-03-15 15:56:46
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
最新评论