天天看點

getClass().getResourceAsStream() .擷取配置檔案的方法

class.getResourceAsStream 用法

 首先,Java中的getResourceAsStream有以下幾種 Class.getResourceAsStream(String path) : path 不以’/‘開頭時預設是從此類所在的包下取資源,以’/‘開頭則是從

  ClassPath根下擷取。其隻是通過path構造一個絕對路徑,最終還是由ClassLoader擷取資源。

  2. Class.getClassLoader.getResourceAsStream(String path) :預設則是從ClassPath根下擷取,path不能以’/‘開頭,最終是由

  ClassLoader擷取資源。

  3. ServletContext. getResourceAsStream(String path):預設從WebAPP根目錄下取資源,Tomcat下path是否以’/‘開頭無所謂,

  當然這和具體的容器實作有關。

  4. Jsp下的application内置對象就是上面的ServletContext的一種實作。

  其次,getResourceAsStream 用法大緻有以下幾種

  第一: 要加載的檔案和.class檔案在同一目錄下,例如:com.x.y 下有類me.class ,同時有資源檔案myfile.xml

  那麼,應該有如下代碼

  me.class.getResourceAsStream("myfile.xml");

  第二:在me.class目錄的子目錄下,例如:com.x.y 下有類me.class ,同時在 com.x.y.file 目錄下有資源檔案myfile.xml

  那麼,應該有如下代碼

  me.class.getResourceAsStream("file/myfile.xml");

  第三:不在me.class目錄下,也不在子目錄下,例如:com.x.y 下有類me.class ,同時在 com.x.file 目錄下有資源檔案myfile.xml

  那麼,應該有如下代碼

  me.class.getResourceAsStream("/com/x/file/myfile.xml");

  總結一下,可能隻是兩種寫法

  第一:前面有 “ / ”

  “ / ”代表了工程的根目錄,例如工程名叫做myproject,“ / ”代表了myproject

  me.class.getResourceAsStream("/com/x/file/myfile.xml");

  第二:前面沒有 “ / ”

  代表目前類的目錄

  me.class.getResourceAsStream("myfile.xml");

  me.class.getResourceAsStream("file/myfile.xml");

src(源檔案夾)

┣━11.properties

┗━myspider(myspider包)

         ┃

         ┣━22.properties

         ┗━Test.java

package myspider;  
  
import java.io.UnsupportedEncodingException;  
  
/** 
 * 
 * @author mark 
 */  
public class Test {  
    public static void main(String[] args) throws UnsupportedEncodingException{  
        Test t=new Test();  
        //檔案名前不加“/”,則表示從目前類所在的包下查找該資源。如下則表示的是從包myspider下查找22.properties檔案資源。  
        System.out.println("1:"+t.getClass().getResourceAsStream("22.properties"));//輸出[email protected]  
  
        //檔案名前加了“/”,則表示從類路徑下也就是從classes檔案夾下查找資源,如下表示從classes檔案夾下查找22.properties檔案資源。  
        System.out.println("2:"+t.getClass().getResourceAsStream("/22.properties"));//輸出null  
  
        //檔案名前加了“/”,則表示從類路徑下也就是從classes檔案夾下查找資源,如下表示從classes檔案夾下查找11.properties檔案資源。  
        System.out.println("3:"+t.getClass().getResourceAsStream("/11.properties"));//輸出[email protected]  
        System.out.println();  
  
        //目前包路徑4:file:/E:/myobject/myspider/build/classes/myspider/  
        System.out.println("4:"+t.getClass().getResource(""));  
  
        //輸出目前類路徑5:file:/E:/myobject/myspider/build/classes/  
        System.out.println("5:"+t.getClass().getResource("/"));  
  
        /* 
         * 如果類路徑下的目前包有22.properties檔案,則輸出6:file:/E:/myobject/myspider/build/classes/myspider/22.properties 
         * 否者輸出源檔案下的22.properties檔案的路徑,則輸出:6:file:/E:/myobject/myspider/src/myspider/22.properties 
         */  
        System.out.println("6:"+t.getClass().getResource("22.properties"));  
        /* 
         * 如果類路徑下有11.properties檔案,則輸出7:file:/E:/myobject/myspider/build/classes/11.properties 
         * 否者輸出源檔案下的11.properties檔案的路徑,則輸出:6:7:file:/E:/myobject/myspider/src/11.properties 
         */  
        System.out.println("7:"+t.getClass().getResource("/11.properties"));  
  
    }  
}