我的文件夹结构如下:
>test
>src
>org.mypackage
>myclass
>resources
>config.properties
java类如下:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public myclass {
private String path = "config.properties";
public myclass() {
Properties prop = new Properties();
InputStream input = null;
try {
input = getClass().getClassLoader().getResourceAsStream(path);
prop.load(input)
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}
但是在调试期间我得到 null
作为 input
的值。
在 Debug模式下,我检查了 getClass()
的值,得到了类值,该值也可用于 getClass().getClassLoader()
但是对于 getClass().getClassLoader().getResourceAsStream(path)
来说,它是 null
。
我不确定问题是什么。
感谢您的帮助!
最佳答案
似乎 resources
不是 Eclipse 项目中的源文件夹。尝试将 resources
设置为源文件夹:
Right click > Build Path > Use as Source Folder
关于java - java中不读取config.properties文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40465787/