Class中的getResourceAsStream(String name)
声明:public InputStream getResourceAsStream(String name) Class类中的getResourceAsStream()的其实也是代理给Classloader中的getResourceAsStream(),只是name代表的资源方式不同。
- 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').
程序如下:其中getResource()与getResourceAsStream()是一样的。只是用url打开一个输入流。
package zhao.shu.you; URL url1=class2.getResource(""); System.out.println(url1); URL url2=class2.getResource("/"); System.out.println(url2); ClassLoader class11=class1.getClassLoader(); print:file:/D:/java_temp/plug1/out/production/module1/zhao/shu/you/file:/D:/java_temp/plug1/out/production/module1/复制代码
classLoader
直接是当前包的地址,不用是/开头。如果以“/”显示没有相关资源,返回null。程序如下
ClassLoader class11=class1.getClassLoader(); System.out.println(class11.getResource("")); System.out.println(class11.getResource("")); Print:file:/D:/java_temp/plug1/out/production/module1/null复制代码
参考: oracle的库文件说明。