不使用 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);
} 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);
}