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
Readerunconditionally. - Equivalent to {@link Reader#close()}, except any exceptions will be ignored.
- This is typically used in finally blocks.
- Example code:
-
- 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);
- }
- @param input the Reader to close, may be null or already closed
*/
public static void closeQuietly(final Reader input) {
closeQuietly((Closeable) input);
}
/**
- Closes an
Writerunconditionally. - Equivalent to {@link Writer#close()}, except any exceptions will be ignored.
- This is typically used in finally blocks.
- Example code:
-
- 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);
- }
- @param output the Writer to close, may be null or already closed
*/
public static void closeQuietly(final Writer output) {
closeQuietly((Closeable) output);
}
/**
- Closes an
InputStreamunconditionally. - Equivalent to {@link InputStream#close()}, except any exceptions will be ignored.
- This is typically used in finally blocks.
- Example code:
-
- 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);
- }
- @param input the InputStream to close, may be null or already closed
*/
public static void closeQuietly(final InputStream input) {
closeQuietly((Closeable) input);
}
/**
- Closes an
OutputStreamunconditionally. - Equivalent to {@link OutputStream#close()}, except any exceptions will be ignored.
- This is typically used in finally blocks.
- Example code:
-
- 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);
- }
- @param output the OutputStream to close, may be null or already closed
*/
public static void closeQuietly(final OutputStream output) {
closeQuietly((Closeable) output);
}
/**
- Closes a
Closeableunconditionally. - Equivalent to {@link Closeable#close()}, except any exceptions will be ignored. This is typically used in
- finally blocks.
- Example code:
-
- Closeable closeable = null;
- try {
- closeable = new FileReader("foo.txt");
- // process closeable
- closeable.close();
- } catch (Exception e) {
- // error handling
- } finally {
- IOUtils.closeQuietly(closeable);
- }
- Closing all streams:
-
- try {
- return IOUtils.copy(inputStream, outputStream);
- } finally {
- IOUtils.closeQuietly(inputStream);
- IOUtils.closeQuietly(outputStream);
- }
- @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
}
}
作者:huanghanqian
来源:CSDN
原文:https://blog.csdn.net/huanghanqian/article/details/82500132
版权声明:本文为博主原创文章,转载请附上博客链接!
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
itfan123




