java 在java中读取属性文件时发生文件未找到错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11242007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File not found error occur when read property file in java
提问by AKZap
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.
请让我知道如何获取此属性文件。
回答by Jigar Joshi
Try with
试试
prop.load(getClass().getResourceAsStream("resources/common/configure/commonData.properties"));
回答by Satbir
The program tries to find the "commonData.properties" at a path specified relative to where you are running it. Providing a correct relative path or full path of configuration file might solve the issue.
该程序尝试在相对于您运行它的位置指定的路径中找到“commonData.properties”。提供正确的配置文件的相对路径或完整路径可能会解决问题。
回答by carlspring
Use absolute file paths. Print the full path and you'll be able to spot your problem.
使用绝对文件路径。打印完整路径,您将能够发现您的问题。
Alternatively, use getClass().getResourceAsStream()
.
或者,使用getClass().getResourceAsStream()
.