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

时间:2012-07-11 10:08:40

标签: java

我有以下代码:

    // 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?如果是的话,我该怎么做?我的代码清单中出现了错误的练习错误。要求它最终阻止。

4 个答案:

答案 0 :(得分:12)

您必须关闭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");
}

答案 1 :(得分:2)

你应该总是关闭你的流,并在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 :(得分:1)

这是一个例子:

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

答案 3 :(得分: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");
 }