/**
* Finds a resource with a given name. This method returns null if no
* resource with this name is found. The rules for searching resources
* associated with a given class are implemented by the * defining class
* loader of the class.
*
* <p> This method delegates the call to its class loader, after making
* these changes to the resource name: if the resource name starts with
* "/", it is unchanged; otherwise, the package name is prepended to the
* resource name after converting "." to "/". If this object was loaded by
* the bootstrap loader, the call is delegated to
* <code>ClassLoader.getSystemResource</code>.
*
* @param name name of the desired resource
* @return a <code>java.net.URL</code> object.
* @see java.lang.ClassLoader
* @since JDK1.1
*/
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);
}
* Finds a resource with a given name. This method returns null if no
* resource with this name is found. The rules for searching resources
* associated with a given class are implemented by the * defining class
* loader of the class.
*
* <p> This method delegates the call to its class loader, after making
* these changes to the resource name: if the resource name starts with
* "/", it is unchanged; otherwise, the package name is prepended to the
* resource name after converting "." to "/". If this object was loaded by
* the bootstrap loader, the call is delegated to
* <code>ClassLoader.getSystemResource</code>.
*
* @param name name of the desired resource
* @return a <code>java.net.URL</code> object.
* @see java.lang.ClassLoader
* @since JDK1.1
*/
public java.net.URL getResource(String name) {
name = resolveName(name);
ClassLoader cl = getClassLoader0();
if (cl==null) {
// A system class.
return ClassLoader.getSystemResource(name);
}
return cl.getResource(name);
}
如果写相对的,那么相对哪个路径?资源文件应该放到什么路径?
经过我的试验,发现,资源要放到与类相同的路径中,相对路径
是相对于类所在的目录。所以我认为这个定位方法不是很好。
还是使用System.getProperty("user.dir")定为要好些。