Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

When it comes time to close the fileInputStream, I think I read on Wikipedia that it is good to have it in a finally block. Because it honestly works just fine in try/catch block.

Can you show me correction to get fileInputStream.close() in a finally section?

ConfigProperties.java package base;

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

Is the solution only to do as Eclipse suggests and do this in the finally block?

finally {
    try {
        fileInputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
like image 420
JoJo Avatar asked Jul 24 '12 00:07

JoJo


People also ask

Does FileInputStream need to be closed?

close() method. After any operation to the file, we have to close that file.

Why you need to close the streams in finally block?

The finally block is mostly used during exception handling with try and catch to close streams and files. The code in finally block is executed irrespective of whether there is an exception or not. This ensures that all the opened files are properly closed and all the running threads are properly terminated.

Does try with resources automatically close?

The Java try with resources construct, AKA Java try-with-resources, is an exception handling mechanism that can automatically close resources like a Java InputStream or a JDBC Connection when you are done with them. To do so, you must open and use the resource within a Java try-with-resources block.

How do I close a Finally block stream?

It provides a closeQuitetly() method to close streams quietly i.e. above finally block can be re-written by using IOUtils. closeQuietly() as following.


2 Answers

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
}
like image 198
Jeffrey Avatar answered Nov 04 '22 06:11

Jeffrey


Because FileInputStream.close() throws an IOException, and the finally{} block doesn't catch exceptions. So you need to either catch it or declare it in order to compile. Eclipse's suggestion is fine; catch the IOException inside the finally{} block.

like image 38
Jon Lin Avatar answered Nov 04 '22 05:11

Jon Lin