Создать config.properties в Java с IntelliJ - PullRequest
0 голосов
/ 19 февраля 2020

Я пытался создать config.properties для моего проекта, но я не понимаю, где мне нужно разместить мой класс ReadFile. Мой файл config.properties находится в папке resources , а мой класс ReadFile расположен в sr c.

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

}
}

И ошибка:

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)
...