简体   繁体   English

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

[英]File not found error occur when read property file in java

i create the property file under package of resources/common/configure/ 我在resources/common/configure/包下创建属性文件

then i create the code 然后我创建代码

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

but i got the following error 但我得到以下错误

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

please let me know how can i get this property file. 请让我知道如何获得此属性文件。

试试吧

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

The program tries to find the "commonData.properties" at a path specified relative to where you are running it. 程序试图在相对于运行它的位置指定的路径中找到“commonData.properties”。 Providing a correct relative path or full path of configuration file might solve the issue. 提供正确的相对路径或配置文件的完整路径可能会解决此问题。

Use absolute file paths. 使用绝对文件路径。 Print the full path and you'll be able to spot your problem. 打印完整路径,您将能够发现您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM