【问题标题】:do I need to surround fileInputStream.close with a try/catch/finally block? How is it done?我需要用 try/catch/finally 块包围 fileInputStream.close 吗?它是如何完成的?
【发布时间】:2012-07-24 00:10:35
【问题描述】:

我有以下 Java 类,它做一件事,从 config.properties 中触发值。

当需要关闭 fileInputStream 时,我想我在 Wikipedia 上读到,最好将它放在 finally 块中。因为它在 try/catch 块中确实可以正常工作。

你能告诉我在 finally 部分中得到fileInputStream.close() 的更正吗?

ConfigProperties.java 包基础;

import java.io.FileInputStream;
import java.util.Properties;

public class ConfigProperties {

    public FileInputStream fileInputStream;
    public String property;

    public String getConfigProperties(String strProperty) {

        Properties configProperties = new Properties();
        try {

            fileInputStream = new FileInputStream("resources/config.properties");
            configProperties.load(fileInputStream);
            property = configProperties.getProperty(strProperty);
            System.out.println("getConfigProperties(" + strProperty + ")");

            // use a finally block to close your Stream.
            // If an exception occurs, do you want the application to shut down?

        } catch (Exception ex) {
            // TODO
            System.out.println("Exception: " + ex);
        }
        finally {
            fileInputStream.close();
        }

        return property;
    }
}

解决方案是否只能按照 Eclipse 的建议执行并在 finally 块中执行?

finally {
    try {
        fileInputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

【问题讨论】:

  • 仅供参考:commons-io 有一个很好的功能:IOUtils.closeQuietly() 将处理关闭时的 try/catch。您的代码会看起来更好:) 请记住,此函数不会让您有机会响应 IOException,但通常人们不会这样做并且无论如何都会忽略该异常。如果您需要响应,请创建自己的实用程序方法来记录或打印堆栈跟踪或您需要做的任何事情。

标签: java oop try-catch-finally


【解决方案1】:

是的,这是 Java 7 之前的常见解决方案。但是,随着 Java 7 的引入,现在有 try-with-resource 语句,当 try 块退出时,它们会自动关闭所有声明的资源:

try (FileInputStream fileIn = ...) {
    // do something
} // fileIn is closed
catch (IOException e) {
    //handle exception
}

【讨论】:

    【解决方案2】:

    因为FileInputStream.close() 会抛出 IOException,而 finally{} 块不会捕获异常。因此,您需要捕获它或声明它才能编译。 Eclipse 的建议很好;在 finally{} 块中捕获 IOException。

    【讨论】:

    • 您也可以使用 Commons IO 中的 IOUtils.closeQuietly() 方法。它包装了 close 方法以删除所有抛出的 IOExceptions。比使用另一个 try/catch 块更干净。
    【解决方案3】:

    标准方法是:

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(...);
        // do something with the inputstream
    } catch (IOException e) {
        // handle an exception
    } finally { //  finally blocks are guaranteed to be executed
        // close() can throw an IOException too, so we got to wrap that too
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }        
        } catch (IOException e) {
            // handle an exception, or often we just ignore it
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 2012-11-12
      • 2014-11-27
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 2015-09-05
      • 2015-02-15
      • 2012-03-06
      相关资源
      最近更新 更多