getResourceAsStream()是java.lang.Class类的方法.此方法将具有给定名称的资源查找到类路径中.实际上这个方法委托给这个对象的类加载器.在这个示例PropUtil对象的类加载器中.但在委托之前,使用以下算法从给定的资源名称构造绝对资源名称.
Kev*_*edi 63
如果使用静态方法并从classpath文件夹加载属性文件,则可以使用以下代码:
//load a properties file from class path, inside static method
Properties prop = new Properties();
prop.load(Classname.class.getClassLoader().getResourceAsStream("foo.properties"));
Run Code Online (Sandbox Code Playgroud)
old*_*inb 56
final Properties properties = new Properties();
try (final InputStream stream =
this.getClass().getResourceAsStream("foo.properties")) {
properties.load(stream);
/* or properties.loadFromXML(...) */
}
Run Code Online (Sandbox Code Playgroud)