使用Eclipse开发,将properties文件放在src文件夹下(生成class时,properties文件也会拷贝到bin下)
使用ClassLoader.getSystemResourceAsStream方法得到InputStream,
再用Properties的load方法加载属性文件
package ex.properties;
import java.io.IOException;
import java.util.Properties;
public class LoadProperties {
public static void main(String[] args) throws IOException {
//String myPath = System.getProperty("user.dir");
Properties p = new Properties();
p.load(ClassLoader.getSystemResourceAsStream("Hello.properties"));
System.out.println(p.getProperty("word"));
}
}
这样就不会出现找不到文件的情况出现了。