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

相關文章
相關標籤/搜索