大神进,菜鸟请勿打扰

Properties p=new Properties();

第一种方法: p.load(new FileInputStream("database.properties"));
第二种方法: p.load(BaseDao.class.getClassLoader().getResourceAsStream("database.properties"));

第一种方法会报错
java.io.FileNotFoundException: database.properties (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at com.Pet.Tool.BaseDao.bb(BaseDao.java:28)
at com.Pet.Tool.BaseDao.(BaseDao.java:21)
at com.Pet.text.text.main(text.java:14)
第二种方法则可以通过

请教:俩种方法的区别?? 为什么第一种方法会报错???请教大神

第一次见到问问题这么牛B的,萌新瑟瑟发抖中
图片说明

详解:https://zhidao.baidu.com/question/616631012276519492.html

public FileInputStream(String name) throws FileNotFoundException {
    this(name != null ? new File(name) : null);
}

这是FileInputStream的底层代码,你可以看到,你new FileInputStream时,他会调用new File,创建一个新的File ,这个File是以你的name为路
径进行创建的,并不是你原来的properties文件,你把name改成相对路径试下,如果对了,也有可能找不见内容,因为是新的文件。
我也是菜鸟,如果不对还望海涵,错误之处帮忙指出。
下面是new File的底层
public File(String pathname) {
if (pathname == null) {
throw new NullPointerException();
}
this.path = fs.normalize(pathname);
this.prefixLength = fs.prefixLength(this.path);
}