Java 如何从POJO从meta inf文件夹读取属性文件
如何从纯java类中从web应用程序的meta inf文件夹中读取属性文件。最简单的方法是:-Java 如何从POJO从meta inf文件夹读取属性文件,java,jar,properties,package,meta-inf,Java,Jar,Properties,Package,Meta Inf,如何从纯java类中从web应用程序的meta inf文件夹中读取属性文件。最简单的方法是:- InputStream propertiesIs = this.getClass().getClassLoader().getResourceAsStream("META-INF/your.properties"); Properties prop = new Properties(); prop.load(propertiesIs); System.out.println(prop.getPrope
InputStream propertiesIs = this.getClass().getClassLoader().getResourceAsStream("META-INF/your.properties");
Properties prop = new Properties();
prop.load(propertiesIs);
System.out.println(prop.getProperty(YourPropertyHere));
或
您还可以尝试使用FileInputStream
加载属性:-
input = new FileInputStream("META-INF/your.properties");
这是给我的。在阅读资源时,应注意正确关闭它们
InputStream streamOrNull = getClass().getClassLoader().getResourceAsStream(
"META-INF/your.properties");
if (streamOrNull == null) {
// handle no such file
}
else {
try (InputStream stream = streamOrNull) { // close stream eventually
Properties properties = new Properties();
properties.load(stream);
// access properties
}
}
请包括您尝试过的代码。还请包括您遇到的任何错误。@sahoo1989这就是为什么我们应该在问题描述中给出我们迄今为止尝试过的内容&当前代码/结构缺少
/
。至少对我来说,这似乎是必需的。
InputStream streamOrNull = getClass().getClassLoader().getResourceAsStream(
"META-INF/your.properties");
if (streamOrNull == null) {
// handle no such file
}
else {
try (InputStream stream = streamOrNull) { // close stream eventually
Properties properties = new Properties();
properties.load(stream);
// access properties
}
}