我正在使用 Maven Web 应用程序。
以下是我放置属性文件的项目结构。
以下是我用来读取此文件的代码:
public static Properties loadProducerProperty() throws FileException {
Properties myProperties = new Properties();
try {
String resourceName = "newproperties.properties"; // could also be a constant
ClassLoader loader = Thread.currentThread().getContextClassLoader();
try (InputStream resourceStream = loader.getResourceAsStream(resourceName)) {
myProperties.load(resourceStream);
}
} catch (FileNotFoundException e) {
throw new FileException();
} catch (IOException e) {
throw new FileException();
}
return myProperties;
}
但我得到 FileNotFound
异常
我已经浏览了以下链接并尝试了其他操作,但我收到了相同的错误:
Cannot load properties file from resources directory
我在这里做错了什么。
谢谢
最佳答案
在文件名前面添加斜杠:
String resourceName = "/newproperties.properties"; // could also be a constant
关于java - 未找到 Maven 属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38740180/