带有属性文件的 FileNotFoundException [英] FileNotFoundException with properties file
问题描述
我正在尝试从 backupData.properties
读取一些道具.它位于WEB-INF/classes/
.我就是这样做的:
I'm trying to read some props from backupData.properties
. It locates in WEB-INF/classes/
. This is how I do:
public class Configures {
private static final String INPUT_FILE = "WEB-INF//classes//backupData.properties";
public static String getMail() {
Properties prop = new Properties();
try {
//load a properties file
prop.load(new FileInputStream(INPUT_FILE));
//get the property value
return prop.getProperty("mail");
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
}
INPUT_FILE
应该包含什么?我试图把它放在 src
中,比如 src//backupData.properties
,但它抛出 FileNotFoundException
.我用谷歌搜索该文件应该位于 CLASSPATH
(据我所知在 WEB-INF/classes
中).怎么了?
What INPUT_FILE
should contain? I was trying to put it in src
, like src//backupData.properties
, but it throws FileNotFoundException
. I googled that file should locate in CLASSPATH
(in WEB-INF/classes
as I understood). What is wrong?
附注.我正在使用 Spring.
推荐答案
这与 Spring 无关.如果您正在部署 Web 应用程序,WEB-INF/classes
中的所有内容都将从类路径的根目录开始显示.
This has nothing to do with Spring. If you are deploying a web application, everything in WEB-INF/classes
will appear starting at the root of the classpath.
您可以使用
InputStream in = Configures.class.getResourceAsStream("/backupData.properties");
prop.load(in);
由于 Web 应用程序并不总是从其 .war
文件中提取,因此实际的属性文件可能仅作为 zip 条目存在.因此,您不能(也不应该)使用 FileInputStream
检索它.
Since a web application is not always extracted from its .war
file, the actual properties file might only exist as a zip entry. As such, you can't (and shouldn't) retrieve it with FileInputStream
.
这篇关于带有属性文件的 FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!