在使用 stream 的時,每每要 try catch IOException。eric教導我要把流的關閉放到 finally 中去寫,而且在 close 以前要判斷一下是否爲 null。可是 stream.close() 也會 throw IOException,這就致使在 finally 中 也須要 try catch 一下,因而代碼就很長。以下:ui
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 來精簡代碼:.net
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 的內部實現以下:code
/**
* 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
}
}
get