如下所示代码:
public class Example041 {
public static void main(String[] args) throws IOException {
Example041 e41 = new Example041();
e41.copy("d:\\微信名ape_it.txt", "d:\\微信名爱题猿.txt");
}
private void copy(String src, String dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int n;
while ((n = is.read(buf)) > 0) {
os.write(buf, 0, n);
}
} finally {
is.close();
os.close();
}
}
}
代码分析:
上述代码正常执行是没有问题的,但是存在隐藏的BUG。BUG就在finally语句块中。首先,is和os在关闭时都没有做是否为空的判断;其次,如果is.close关闭时出错抛出异常了,那么os.close将不会执行,导致的结果就是文件输出流没有关闭。一种对finally语句的修改如下:
if (is != null) {
try {
is.close();
} catch (IOException e) {
// do nothing
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
// do nothing
}
}
上述代码在书写上重复两遍,所以可以进行如下重构:
void copy2(String src, String dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(src);
os = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int n;
while ((n = is.read(buf)) > 0) {
os.write(buf, 0, n);
}
} finally {
close(is);
close(os);
}
}
private void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// do nothing
}
}
}
总之,当你在 finally 语句块中调用 close 方法时,要用一个嵌套的 try-catch语句来保护它,以防止 IOException 的传播。更一般地讲,对于任何在 finally语句块中可能会抛出的被检查异常都要进行处理,而不是任其传播。
注:本【java解惑】系列均是博主阅读《java解惑》原书后将原书上的讲解和例子部分改编然后写成博文进行发布的。所有例子均亲自测试通过并共享在github上。通过这些例子激励自己惠及他人。同时本系列所有博文会同步发布在博主个人微信公众号搜索“爱题猿”或者“ape_it”方便大家阅读。如果文中有任何侵犯原作者权利的内容请及时告知博主以便及时删除如果读者对文中的内容有异议或者问题欢迎通过博客留言或者微信公众号留言等方式共同探讨。
源代码地址https://github.com/rocwinger/java-disabuse
本文出自 “winger” 博客,谢绝转载!