Java properties | FileNotFoundException: properties (系统找不到指定的文件。)

文件存储路径的问题

错误描述 :FileNotFoundException: init.properties (系统找不到指定的文件。)

1.方法一
InputStream fis =TestProperties.class.getClassLoader().getResourceAsStream("init.properties")

2.方法二(要求TestProperties和init.properties在同一目录下)
InputStream fis =TestProperties.class搜索.getResourceAsStream("init.properties")

推荐使用2。 放在TestProperties.java同级目录下面

3.方法三,对于Web工程也可以这样。
先获取ServletContext,然后
InputStream in=context.getResourceAsStream("/WEB-INF/classes/init.properties");

原文地址:https://www.cnblogs.com/slankka/p/9158568.html