Remember to properly close the File!

原文地址:[url=http://www.javablogging.com/remember-to-properly-close-the-file/]http://www.javablogging.com/remember-to-properly-close-the-file/[/url]

现在一些关于怎么样对待java中Files, Streams, Connections 和 其他任何"closeable"对象的一些建议。就简单的以FileWriter类和一段错误使用的它的bug代码:


public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
file.write(content);
} catch (IOException e) {
// Log the exception
}
}



有什么问题吗?哦,很多问题:我们没有验证input值(或许是null或者其他错误数据?),我们没有验证文件是否存在或者是一个问目录,我们没有注意指定文件的编码格式。除此之外你辩解说没有任何问题除了文件本身问题,约定认为:写文件就用FileWriter。也许是对的,但是仍然有一个事情是错的在这段代码中--我们没有 close 这个文件。

这个很重要吗? 哦, 当然! 不关闭这个文件也许的结果就是导致写不能完成并系统一直占用文件不释放。一直要记住:colse这些Files, Streams, Connections!下面是我们经过权宜之后的解决bug的代码:

public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
file.write(content);
file.close();
} catch (IOException e) {
// Log the exeception
}
}


解决了吗?我猜很多人认为它已经解决了。但是事实是没有解决—如果一个异常抛出在写的过程中呢?这个方法将退出,这个文件仍然没有close!好,我们来解决这个问题 — 用try/finally 解决。这种方式可以认为无论什么事情发生都将执行文件的close操作。


public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
file.write(content);
} catch (IOException e) {
// Log the exeception
} finally {
file.close();
}
}


解决了吗? 没有...如果finally块中这个文件将为null,异常仍然会被抛出在FileWriter结构。而且它将是NullPointerException,让我们修复它:


public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
file.write(content);
} catch (IOException e) {
// Log the exeception
} finally {
if (file != null) {
file.close();
}
}
}


完成了吗?仍然很遗憾... file.close() 同样可能抛出异常!即使writeToFile没有任何验证异常在方法中抛出,我们任然要这样对付他们:


public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
try {
file.write(content);
} finally {
file.close();
}
} catch (IOException e) {
// Log the exeception
}
}


总之我们已经解决了这个问题修复了bug!注意这地方没不用在文件关闭之前非要验证这个文件是否为null,因为文件为null时候只有唯一在第3行抛出异常的情况下然后直接跳到第十行。当你关闭一个closeable object不是一个小事情,所以[b]记住做这些![/b]

这里的另外一个问题就是可读性。以上代码对你是可以理解的,然后尝试并非一种closeable对象 - 那将会非常丑!因此你是否对付closeable对象[b]最好的办法是把代码分解成两个方法:一个处理异常和关闭,另外的是处理业务主逻辑[/b]。想了解以上说法的具体代码可以看看下面:


public void writeToFile(String fileName, String content) {
try {
FileWriter file = new FileWriter(fileName);
try {
performWriteToFile(file, content)
} finally {
file.close();
}
} catch (IOException e) {
// Log the exeception
}
}


// Here we perform all the operations on the file.
// This method usually is much longer.
private void performWriteToFile(FileWriter file,
String content) throws IOException {
file.write(content);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值