java 嵌套流关闭_在Java中关闭嵌套流和写入器的正确方法[复制]

我通常做以下的事情。首先,定义一个基于模板方法的类来处理Try/Catch混乱

import java.io.Closeable;

import java.io.IOException;

import java.util.LinkedList;

import java.util.List;

public abstract class AutoFileCloser {

// the core action code that the implementer wants to run

protected abstract void doWork() throws Throwable;

// track a list of closeable thingies to close when finished

private List closeables_ = new LinkedList();

// give the implementer a way to track things to close

// assumes this is called in order for nested closeables,

// inner-most to outer-most

protected final T autoClose(T closeable) {

closeables_.add(0, closeable);

return closeable;

}

public AutoFileCloser() {

// a variable to track a "meaningful" exception, in case

// a close() throws an exception

Throwable pending = null;

try {

doWork(); // do the real work

} catch (Throwable throwable) {

pending = throwable;

} finally {

// close the watched streams

for (Closeable closeable : closeables_) {

if (closeable != null) {

try {

closeable.close();

} catch (Throwable throwable) {

if (pending == null) {

pending = throwable;

}

}

}

}

// if we had a pending exception, rethrow it

// this is necessary b/c the close can throw an

// exception, which would remove the pending

// status of any exception thrown in the try block

if (pending != null) {

if (pending instanceof RuntimeException) {

throw (RuntimeException) pending;

} else {

throw new RuntimeException(pending);

}

}

}

}

}

注意“挂起”异常——这处理了这样一种情况:在关闭期间抛出的异常会掩盖我们真正关心的异常。

最后尝试首先从任何修饰流的外部关闭,因此如果有一个BufferedWriter包装了一个文件编写器,我们尝试首先关闭BufferedWriter,如果失败,仍然尝试关闭文件编写器本身。(注意,如果流已经关闭,则close()的可关闭调用定义将忽略该调用)

您可以使用上面的类,如下所示:

try {

// ...

new AutoFileCloser() {

@Override protected void doWork() throws Throwable {

// declare variables for the readers and "watch" them

FileReader fileReader =

autoClose(fileReader = new FileReader("somefile"));

BufferedReader bufferedReader =

autoClose(bufferedReader = new BufferedReader(fileReader));

// ... do something with bufferedReader

// if you need more than one reader or writer

FileWriter fileWriter =

autoClose(fileWriter = new FileWriter("someOtherFile"));

BufferedWriter bufferedWriter =

autoClose(bufferedWriter = new BufferedWriter(fileWriter));

// ... do something with bufferedWriter

}

};

// .. other logic, maybe more AutoFileClosers

} catch (RuntimeException e) {

// report or log the exception

}

使用这种方法,您就不必担心再次尝试/catch/finally来处理关闭文件的问题。

如果这对于您的使用来说太重了,至少要考虑遵循Try/Catch和它使用的“挂起”变量方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值