I\'d like to know the difference between the following two:
MyClass.class.getClassLoader().getResourceAsStream(\"path/to/my/properties\");
and
From the Javadoc for Class.getResourceAsStream():
This method delegates to this object's class loader. Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
- If the
name
begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.- Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').
In other words, they do the same thing if the "path" begins with a "/", but if not, then in the latter case, the path will be relative to the class's package, whereas the classloader one will be absolute.
In short, the first fetches path/to/my/properties
and the second fetches package/of/myclass/path/to/my/properties
.