我的文件夹结构是:
Main
|
.idea
resources
src
|
main
|
java
|
com.read.properties
|
config.properties
a.java
我在a.java中的代码是:
Properties prop = new Properties();
InputStream input = null;
try {
String filename = "config.properties";
input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return Response.status(400).build();
}
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我无法读取属性文件,我不断收到 IO 错误。我尝试更改默认类路径
来自运行->编辑配置
。
当我在 eclipse 中尝试类似的操作时,我可以很容易地读取该文件,这让我相信我在 intelliJ 中的某些配置是错误的。如果有人能指出我正确的方向,我将非常感激
最佳答案
我对此不确定,但在我的 IDEA (Maven) 项目中,我将资源保存在 src/main|test/resources
下,我可以很好地阅读它们:
src
main
resources
path
file
getClass().getClassLoader().getResourceAsStream("path/file");
您需要指定文件的“完整”路径。
关于java - 无法读取 IntelliJ 中的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023931/