在JAVA EE中找不到配置文件
我正在尝试加载配置文件。但它不起作用。我的配置文件放在WEB-INF文件夹下 下面是我加载conf文件的代码:在JAVA EE中找不到配置文件,java,jakarta-ee,Java,Jakarta Ee,我正在尝试加载配置文件。但它不起作用。我的配置文件放在WEB-INF文件夹下 下面是我加载conf文件的代码: private static final String PROPERTIES_FILE = "/WEB-INF/dao.properties"; ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream fichierProperties = classlo
private static final String PROPERTIES_FILE = "/WEB-INF/dao.properties";
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream fichierProperties = classloader.getResourceAsStream(PROPERTIES_FILE);
if (fichierProperties == null) {
throw new DAOConfigurationException("file "+PROPERTIES_FILE+ " not found" );
}
我总是找不到这个错误文件,应该在构建路径上做一些更改???为了简单起见,请尝试 将dao.properties放在src文件夹中,将源代码放在该文件夹中 改为
private static final String PROPERTIES_FILE = "dao.properties"; // <-------
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream fichierProperties = classloader.getResourceAsStream(PROPERTIES_FILE);
if (fichierProperties == null) {
throw new DAOConfigurationException("file "+PROPERTIES_FILE+ " not found" );
}
如果将文件放在WEB-INF目录中,如果您有权访问servlet上下文,则可以使用context对象读取文件,如图所示
InputStream input = context.getResourceAsStream("/WEB-INF/dao.properties");
谢谢你的回答!但是我的配置文件将放在src文件夹中的一个安全的地方?使用classpath:您可以搜索Google,例如使用classpath和关键字:java classpath属性。我正在为你寻找一个很好的例子:和