class.getResource()的用法



 

class.getResource()的用法

用JAVA獲取文件,聽似簡單,但對於很多像我這樣的新人來說,還是掌握頗淺,用起來感覺頗深,大常最經常用的,就是用JAVA的File類,如要取得c:/test.txt文件,就會這樣用File file = newFile("c:/test.txt");這樣用有什麼問題,相信大家都知道,就是路徑硬編碼,對於JAVA精神來說,應用應該一次成型,到處可用,並且從現實應用來講,最終生成的應用也會部署到Windows外的操作系統中,對於linux來說,在應用中用了c:/這樣的字樣,就是失敗,所以,我們應該儘量避免使用硬編碼,即直接使用絕對路徑。 

  在Servlet應用中,有一個getRealPath(String str)的方法,這個方法儘管也可以動態地獲得文件的路徑,不祕直接手寫絕對路徑,但這也是一個不被建議使用的方法,那麼,我們有什麼方法可以更好地獲得文件呢? 

     那就是Class.getResource()與Class.getResourceAsStream()方法,但很多人還是不太懂它的用法,因爲很多人(比如不久前的我)都不知道應該傳怎麼樣的參數給它,當然,有些人己經用得如火純青,這些人是不需要照顧的,在此僅給不會或者還不是很熟的人解釋一點點。 


比如我們有以下目錄 
|--project 
    |--src 
        |--javaapplication 
            |--Test.java 
            |--file1.txt 
        |--file2.txt 
    |--build 
        |--javaapplication 
            |--Test.class 
            |--file3.txt 
        |--file4.txt 

在上面的目錄中,有一個src目錄,這是JAVA源文件的目錄,有一個build目錄,這是JAVA編譯後文件(.class文件等)的存放目錄 
那麼,我們在Test類中應該如何分別獲得 
file1.txt file2.txt file3.txt file4.txt這四個文件呢? 

首先講file3.txt與file4.txt 
file3.txt: 
方法一:File file3 = new File(Test.class.getResource("file3.txt").getFile()); 
方法二:File file3 = new File(Test.class.getResource("/javaapplication/file3.txt").getFile()); 
方法三:File file3 = new File(Test.class.getClassLoader().getResource("javaapplication/file3.txt").getFile()); 

file4.txt: 
方法一:File file4 = new File(Test.class.getResource("/file4.txt").getFile()); 
方法二:File file4 = new File(Test.class.getClassLoader().getResource("file4.txt").getFile()); 

很好,我們可以有多種方法選擇,但是file1與file2文件呢?如何獲得? 
答案是,你只能寫上它們的絕對路徑,不能像file3與file4一樣用class.getResource()這種方法獲得,它們的獲取方法如下 
假如整個project目錄放在c:/下,那麼file1與file2的獲取方法分別爲 
file1.txt 
方法一:File file1 = new File("c:/project/src/javaapplication/file1.txt"); 
方法二:。。。沒有 

file2.txt 
方法一:File file2 = new File("c:/project/src/file2.txt"); 
方法二:。。。也沒有 

總結一下,就是你想獲得文件,你得從最終生成的.class文件爲着手點,不要以.java文件的路徑爲出發點,因爲真正使用的就是.class,不會拿個.java文件就使用,因爲java是編譯型語言嘛 

至於getResouce()方法的參數,你以class爲出發點,再結合相對路徑的概念,就可以準確地定位資源文件了,至於它的根目錄嘛,你用不同的IDEbuild出來是不同的位置下的,不過都是以頂層package作爲根目錄,比如在Web應用中,有一個WEB-INF的目錄,WEB-INF目錄裏面除了web.xml文件外,還有一個classes目錄,沒錯了,它就是你這個WEB應用的package的頂層目錄,也是所有.class的根目錄“/”,假如clasaes目錄下面有一個file.txt文件,它的相對路徑就是"/file.txt",如果相對路徑不是以"/"開頭,那麼它就是相對於.class的路徑。。 

還有一個getResourceAsStream()方法,參數是與getResouce()方法是一樣的,它相當於你用getResource()取得File文件後,再new InputStream(file)一樣的結果 

   

class.getResource("/") --> 返回class文件所在的頂級目錄,一般爲包名的頂級目錄。 --> file:/home/duanyong/workspace/cxxx/xxxx/bin/WEB-INF/classes/ 
class.getResource("/xxx.txt") --> 返回頂級目錄下的xxx.txt路徑。 file://..../bin/WEB-INF/classes/xxx.txt 

getResource(String path),path是以class文件的頂級目標所在的相對路徑。如果頂級目錄爲classes,在classes/xxx/yyy.txt這樣一個文件。取得yyy.txt的語法爲:class.getResource("/xxx/yyy.txt"); 

示例代碼: 
  1. //取得classes頂級目錄下的/xxx/yyy.txt文件   
  2. System.out.println(Test.class.getResource("/xxx/yyy.txt"));   
  3. //取得本class的上路徑   
  4. System.out.println(Test.class.getResource(Test.class.getSimpleName() + ".class"));          
[Java] view plaincopy
  1. //取得classes頂級目錄下的/xxx/yyy.txt文件  
  2. System.out.println(Test.class.getResource("/xxx/yyy.txt"));  
  3. //取得本class的上路徑  
  4. System.out.println(Test.class.getResource(Test.class.getSimpleName() + ".class"));          

The InputStream is typically a FileInputStream. However, if you've jarred up your web application, instead of using a FileInputStream, the InputStream would need to come from the JAR file. If that is the case, then you need to ask the class loader for that stream, as shown here: 

InputStream is =
this.getClass().getClassLoader().getResourceAsStream(
"foo.properties");

Properties props = new Properties();
try {
props.load(is);
String value = (String)props("hello");
System.out.println("Value of hello is: " + value);

} catch (IOException e) {
System.err.println("Load failed");
}


結果: 
file:/home/duanyong/workspace/test/bin/WEB-INF/classes/xxx/yyy.txt 
file:/home/duanyong/workspace/test/bin/WEB-INF/classes/cn/duanyong/test/Test.class
發佈了34 篇原創文章 · 獲贊 62 · 訪問量 182萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章