I'd like to know the difference between the following two:
MyClass.class.getClassLoader().getResourceAsStream("path/to/my/properties");
and
MyClass.class.getResourceAsStream("path/to/my/properties");
Thank you.
From the Class.getClassLoader()
documentation:
Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.
So getClassLoader()
may return null
if the class was loaded by the bootstrap class loader, hence the null check in the Class.getResourceAsStream
implementation:
public InputStream getResourceAsStream(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResourceAsStream(name);
}
return cl.getResourceAsStream(name);
}
You'll also note the statement name = resolveName(name);
which Mark Peters has explained in his answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With