读取属性文件时,getResourceAsStream返回null [英] getResourceAsStream returns null when reading properties file
本文介绍了读取属性文件时,getResourceAsStream返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从Maven资源文件夹中加载application.properties文件,但getResourceAsStream
返回空.这是我的代码:
I want to load application.properties file form maven resource folder but getResourceAsStream
returns null. here is my code:
static {
Properties props = new Properties();
InputStream in = Configuration.class.getResourceAsStream("application.properties");
props.load(in);
}
但InputStream
为空. Configuration
类位于org.elasticsearch.utils
包中,而application.properties
位于src/main/resources
中.怎么了?
but InputStream
is null. Configuration
class is located at org.elasticsearch.utils
package and application.properties
is located at src/main/resources
. What is wrong?
推荐答案
您正在尝试读取相对于Configuration
类的类路径资源.
You're trying to read a classpath resource relative to the Configuration
class.
- 如果要执行此操作,则资源必须位于相同的相对路径上,即,属性文件必须位于
src/main/resources/org/elasticsearch/utils
中. - 或者您也可以使用绝对路径:
/application.properties
.
- If you want to do this, the resource must be on the same relative path, i.e. your property file must be in
src/main/resources/org/elasticsearch/utils
. - Or you can instead use absolute path:
/application.properties
.
这篇关于读取属性文件时,getResourceAsStream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文