在Java中关闭嵌套流和编写器的正确方法

该问题及其大部分答案可以追溯到Java 7发行之前。Java7 提供了自动资源管理功能来实现此目的。如果您使用的是Java 7或更高版本,则应参考Ross Johnson的答案。


什么是关闭Java中嵌套流的最佳,最全面的方法?例如,考虑设置:


FileOutputStream fos = new FileOutputStream(...)

BufferedOS bos = new BufferedOS(fos);

ObjectOutputStream oos = new ObjectOutputStream(bos);

我知道需要对close操作进行保险(可能通过使用finally子句)。我想知道的是,是否有必要明确确保嵌套流已关闭,还是足以确保关闭外部流(oos)?


我注意到的一件事,至少是在处理此特定示例时,发现内部流似乎仅抛出FileNotFoundExceptions。这似乎暗示着从技术上讲,如果它们失败了,就不必担心将其关闭。


这是一位同事写的:


从技术上讲,如果正确实施,关闭最外面的流(oos)应该就足够了。但是实现似乎有缺陷。


示例:BufferedOutputStream从FilterOutputStream继承close(),后者将其定义为:


 155       public void close() throws IOException {

 156           try {

 157             flush();

 158           } catch (IOException ignored) {

 159           }

 160           out.close();

 161       }

但是,如果flush()由于某种原因引发运行时异常,则永远不会调用out.close()。因此,大多数情况下担心关闭FOS(使文件保持打开状态)似乎“最安全”(但很丑)。


当您绝对需要确保关闭嵌套流时,什么才是最好的选择?


是否有任何官方Java / Sun文档对此进行了详细介绍?


犯罪嫌疑人X
浏览 603回答 3
3回答

阿晨1998

我通常会执行以下操作。首先,定义一个基于模板方法的类来处理try / catch混乱import java.io.Closeable;import java.io.IOException;import java.util.LinkedList;import java.util.List;public abstract class AutoFileCloser {&nbsp; &nbsp; // the core action code that the implementer wants to run&nbsp; &nbsp; protected abstract void doWork() throws Throwable;&nbsp; &nbsp; // track a list of closeable thingies to close when finished&nbsp; &nbsp; private List<Closeable> closeables_ = new LinkedList<Closeable>();&nbsp; &nbsp; // give the implementer a way to track things to close&nbsp; &nbsp; // assumes this is called in order for nested closeables,&nbsp; &nbsp; // inner-most to outer-most&nbsp; &nbsp; protected final <T extends Closeable> T autoClose(T closeable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closeables_.add(0, closeable);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return closeable;&nbsp; &nbsp; }&nbsp; &nbsp; public AutoFileCloser() {&nbsp; &nbsp; &nbsp; &nbsp; // a variable to track a "meaningful" exception, in case&nbsp; &nbsp; &nbsp; &nbsp; // a close() throws an exception&nbsp; &nbsp; &nbsp; &nbsp; Throwable pending = null;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doWork(); // do the real work&nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable throwable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pending = throwable;&nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // close the watched streams&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Closeable closeable : closeables_) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (closeable != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; closeable.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Throwable throwable) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pending == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pending = throwable;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if we had a pending exception, rethrow it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // this is necessary b/c the close can throw an&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // exception, which would remove the pending&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // status of any exception thrown in the try block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pending != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (pending instanceof RuntimeException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw (RuntimeException) pending;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(pending);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}请注意“待处理”异常-处理关闭期间抛出的异常会掩盖我们可能真正关心的异常的情况。最终尝试首先从任何装饰的流的外部关闭,因此,如果您有包裹FileWriter的BufferedWriter,我们将尝试首先关闭BuffereredWriter,如果失败,仍然尝试关闭FileWriter本身。(请注意,Closeable的定义要求close()在流已关闭的情况下忽略该调用)您可以按如下方式使用上述类:try {&nbsp; &nbsp; // ...&nbsp; &nbsp; new AutoFileCloser() {&nbsp; &nbsp; &nbsp; &nbsp; @Override protected void doWork() throws Throwable {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // declare variables for the readers and "watch" them&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileReader fileReader =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; autoClose(fileReader = new FileReader("somefile"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader bufferedReader =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; autoClose(bufferedReader = new BufferedReader(fileReader));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... do something with bufferedReader&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if you need more than one reader or writer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileWriter fileWriter =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; autoClose(fileWriter = new FileWriter("someOtherFile"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedWriter bufferedWriter =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; autoClose(bufferedWriter = new BufferedWriter(fileWriter));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... do something with bufferedWriter&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; // .. other logic, maybe more AutoFileClosers} catch (RuntimeException e) {&nbsp; &nbsp; // report or log the exception}使用这种方法,您再也不必担心try / catch / finally来再次处理关闭文件。如果这对于您的使用来说太重了,至少要考虑遵循try / catch及其使用的“ pending”变量方法。

炎炎设计

关闭链接流时,只需要关闭最外面的流。任何错误都会在整个链中传播并被捕获。有关详细信息,请参考Java I / O流。您的同事对Runtime Exception 提出了很好的建议。如果您绝对需要关闭流,则始终可以尝试从外向内分别关闭每个流,并在第一个例外处停止。

青春有我

在Java 7时代,尝试资源是必经之路。如先前的几个答案中所述,关闭请求从最外面的流传播到最里面的流。因此,只需要一次关闭即可。try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f))) {&nbsp; // do something with ois}但是,这种模式存在问题。try-with-resources无法识别内部的FileInputStream,因此,如果ObjectInputStream构造函数引发异常,则FileInputStream永远不会关闭(直到垃圾收集器到达它为止)。解决方法是...try (FileInputStream fis = new FileInputStream(f); ObjectInputStream ois = new ObjectInputStream(fis)) {&nbsp; // do something with ois}这不那么优雅,但是更强大。这是否实际上是一个问题将取决于在构造外部对象期间可以引发哪些异常。ObjectInputStream会抛出IOException,它很可能由应用程序处理而不会终止。许多流类仅抛出未经检查的异常,这很可能导致应用程序终止。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java