如何在读取属性文件时关闭FileInputStream?

11

我有以下代码:

    // 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个回答

20

你必须关闭 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: " + e.getMessage());
} catch (IOException e) {
    System.out.println("IOEXCeption: " + e.getMessage());
}

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");
}

流会自动关闭。


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;
    }
}

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时,才有“try with resource”新功能。 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");
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接