do I need to surround fileInputStream.close with a try/catch/finally block? How is it done?

后端 未结 3 491
失恋的感觉
失恋的感觉 2021-01-05 06:16

I have the following Java Class that does one thing, fires out values from config.properties.

When it comes time to close the fileInputStream

3条回答
  •  情歌与酒
    2021-01-05 06:53

    Yes, that is the common pre-Java 7 solution. However, with the introduction of Java 7, there are now try-with-resource statements which will automatically close any declared resources when the try block exits:

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

提交回复
热议问题