有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

文件创建配置。使用IntelliJ的Java中的属性

我试图创建一个配置。属性,但我不知道需要将ReadFile类放置在何处。 我的配置。属性文件位于resources文件夹下,而我的ReadFile类位于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();
            }
        }
    }

}
}

错误是:

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)

共 (0) 个答案