10

我有以下代码:

    // Read properties file.
     Properties properties = new Properties();
     try {
     properties.load(new FileInputStream("filename.properties"));
     } catch (FileNotFoundException e) {
     system.out.println("FileNotFound");
     }catch (IOException e) {
     system.out.println("IOEXCeption");
     }

是否需要关闭 FileInputStream?如果是,我该怎么做?我的代码清单中出现了错误的做法。要求它放置 finally 块。

4

4 回答 4

17

您必须关闭FileInputStream,因为Properties实例不会。从Properties.load()javadoc:

此方法返回后,指定的流保持打开状态。

将 存储FileInputStream在一个单独的变量中,在外部声明try并添加一个finally块,FileInputStream如果它被打开则关闭:

Properties properties = new Properties();
FileInputStream fis = null;
try {
    fis = new FileInputStream("filename.properties");
    properties.load(fis);
} catch (FileNotFoundException e) {
    system.out.println("FileNotFound");
} catch (IOException e) {
    system.out.println("IOEXCeption");
} finally {
    if (null != fis)
    {
        try
        {
            fis.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

从 Java 7 开始使用try-with-resources :

final Properties properties = new Properties();
try (final FileInputStream fis =
         new FileInputStream("filename.properties"))
{
    properties.load(fis);
} catch (FileNotFoundException e) {
    system.out.println("FileNotFound");
} catch (IOException e) {
    system.out.println("IOEXCeption");
}
于 2012-07-11T10:12:21.717 回答
3

您应该始终关闭您的流,并且在finally块中执行此操作是一个好习惯。这样做的原因是finally块总是被执行,并且你想确保流总是关闭,即使发生了一些坏事。

    FileInputStream inStream = null;
    try {
        inStream = new FileInputStream("filename.properties");
        properties.load(inStream);
    } catch (FileNotFoundException e) {
        System.out.println("FileNotFound");
    } catch (IOException e) {
        System.out.println("IOEXCeption");
    } finally {
        try {
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

如果您使用的是 Java 7,这会变得容易得多,因为try-with引入了新的语法。然后你可以这样写:

try(FileInputStream inStream = new FileInputStream("filename.properties")){
       properties.load(inStream);
   } catch (FileNotFoundException e) {
        System.out.println("FileNotFound");
   } catch (IOException e) {
        System.out.println("IOEXCeption");
}

并且流自动关闭。

于 2012-07-11T10:14:31.780 回答
2

这是一个例子:

    public class PropertiesHelper {
    public static Properties loadFromFile(String file) throws IOException {
        Properties properties = new Properties();
        FileInputStream stream = new FileInputStream(file);
        try {
            properties.load(stream);
        } finally {
            stream.close();
        }
        return properties;
    }
}
于 2012-07-11T10:14:49.163 回答
1

您可以使用 Lombok @Cleanup 来简单地做到这一点。 http://projectlombok.org/features/Cleanup.html

 Properties properties = new Properties();
 try {
   @Cleanup FileInputStream myFis = new FileInputStream("filename.properties");
   properties.load(myFis);
 } catch (FileNotFoundException e) {
   System.out.println("FileNotFound");
 }catch (IOException e) {
   System.out.println("IOEXCeption");
 }

或者,仅当您使用 Java 7 时,才有“使用资源尝试”新功能。 http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

 Properties properties = new Properties();
 try (FileInputStream myFis = new FileInputStream("filename.properties")) {
   properties.load(myFis);
 } catch (FileNotFoundException e) {
   System.out.println("FileNotFound");
 }catch (IOException e) {
   System.out.println("IOEXCeption");
 }
于 2012-07-11T10:24:29.413 回答