Home » Java » file – Create config.properties in Java with IntelliJ-Exceptionshub

file – Create config.properties in Java with IntelliJ-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I tried to create a config.properties on my project, but I don’t understand where I need to place my ReadFile class.
My config.properties file is situated under resources folder, while my ReadFile class is situated under src.

public class ReadPropertyFile {
public static void main(String[] args) {

    Properties prop = new Properties();
    InputStream input = null;

    try {

        input = new FileInputStream("resources/config.properties");

        // load a properties file
        prop.load(input);

        // get the property value and print it out
        System.out.println(prop.getProperty("database"));
        System.out.println(prop.getProperty("dbuser"));
        System.out.println(prop.getProperty("dbpassword"));

    }
    catch (IOException ex) {
        ex.printStackTrace();
    }
    finally {
        if (input != null) {
            try {
                input.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
}

And the error is:

java.io.FileNotFoundException: resources/config.properties (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at ReadPropertyFile.main(ReadPropertyFile.java:14)
How to&Answers: