標籤:
轉自--->http://ouyangfei0426.iteye.com/blog/1020232
src(源檔案夾)
┃
┣━11.properties
┃
┗━myspider(myspider包)
┃
┣━22.properties
┗━Test.java
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")); } }
java路徑問題(一)——getClass().getResourceAsStream()