class与classloader的getResourceAsStream(String name)

Class中的getResourceAsStream(String name)

声明:public InputStream getResourceAsStream(String name) Class类中的getResourceAsStream()的其实也是代理给Classloader中的getResourceAsStream(),只是name表明的资源方式不一样。java

  1. If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
  2. 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打开一个输入流。bash

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。程序以下oracle

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的库文件说明。ui

相关文章
相关标签/搜索