在网上搜索java如何读取properties配置文件,大概有两种方法:
1.第一种
public class Demo1 {
public void createBase(){
Properties p = new Properties();
try {
InputStream in = new FileInputStream("dbparam.properties");
p.load(in);
System.out.println("6");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这种方法怎么试都不行,抛出无法找到文件的异常
2.第二种
public class Demo1 {
public void createBase(){
Properties p = new Properties();
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("dbparam.properties");
try {
p.load(in);
System.out.println("6");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这种方法就正常了,现在想问下第一种方法为什么不行?这两种方法有什么区别和用法需要注意的吗?