在java中读取属性文件时发生文件未找到错误

时间:2012-06-28 09:47:40

标签: java properties classpath

我在resources/common/configure/

包下创建属性文件

然后我创建代码

    Properties prop = new Properties();

    try {
           //load a properties file
        prop.load(new FileInputStream("resources/common/configure/commonData.properties"));

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


    } catch (IOException ex) {
        ex.printStackTrace();
    }

但我收到以下错误

java.io.FileNotFoundException: (The system cannot find the path specified)

请让我知道如何获取此属性文件。

3 个答案:

答案 0 :(得分:5)

尝试

prop.load(getClass().getResourceAsStream("resources/common/configure/commonData.properties"));

答案 1 :(得分:2)

该程序试图找到" commonData.properties"在相对于您运行它的位置指定的路径上。提供正确的相对路径或配置文件的完整路径可能会解决问题。

答案 2 :(得分:0)

使用绝对文件路径。打印完整路径,您就能发现问题。

或者,使用getClass().getResourceAsStream()