[英]Unable to read a properties file in IntelliJ
我的文件夾結構是:
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錯誤。 我嘗試更改默認的類路徑
從Run->Edit Configurations
。
當我在eclipse中嘗試類似的操作時,我可以很容易地讀取該文件,使我相信我在intellelliJ中有一些配置錯誤。 如果有人能指出正確的方向,我將不勝感激
我對此不確定,但是在我的IDEA(Maven)項目中,我將資源保存在src/main|test/resources
並且我可以很好地閱讀它們:
src
main
resources
path
file
getClass().getClassLoader().getResourceAsStream("path/file");
不過,您需要指定文件的“完整”路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.