java getResourceAsStream的使用方法

來源:互聯網
上載者:User

標籤:起點   str   開頭   word   throws   檔案載入   內建對象   das   tle   

背景:對於java項目中設定檔載入時候的絕對路徑和相對路徑做一個清晰的認識!

1 分析路徑

在Java項目中會經常用到getResourceAsStream這個函數擷取一些設定檔,但是怎樣正確使用這個函數呢?

1.1 熟悉根目錄結構

getResourceAsStream(String path)這個函數的參數是一個路徑,但是這個路徑應該怎麼填?使用該函數的痛點也就在此。

其實只要記住一個關鍵點,getResourceAsStream這個函數尋找檔案的起點是JAVA項目編譯之後的根目錄,比如一般maven項目編譯之後根目錄都是target/classes這個檔案,舉個例子,比如下面這個分類樹就是一個maven項目編譯之後的檔案分布情況:
.
├── auth.server.properties
├── com
│   ├── winwill
│   │   └── test
│   │       ├── TestConstants.class
│   │       ├── TestConstants$HOST.class
│   │       ├── TestGetResourceAsStream.class
│   │       └── TestGuava.class
│   └── xiaomi
│       └── xmpush
│           └── iOS
│               ├── IOSPushTest.class
│               ├── UploadCertificate$1.class
│               └── UploadCertificate.class
├── config
│   └── config2.properties
├── config.properties
├── kafka-consumer.properties
├── kafka-producer.properties
├── META-INF
│   ├── MANIFEST.MF
│   └── maven
│       └── com.winwill.test
│           └── test
│               ├── pom.properties
│               └── pom.xml
├── Redis.properties
└── zookeeper.properties

我們在TestGetResourceAsStream.class這個類中應該怎麼擷取config.properties和config2.properties這兩個檔案呢?

ps:要想查看項目的根目錄結構,可以直接查看項目所在目錄下的classes檔案目錄。例如下面的項目不屬於maven項目

項目結構:

 

編譯後的根目錄結構:

1.2 使用getClass().getResourceAsStream(String path)

ps: path 不以’/‘開頭時預設是從此類所在的包下取資源,以’/‘開頭則是從ClassPath根下擷取。

從上面的分類樹可以看到,config這個檔案的路徑是/config.properties(/表示根目錄),config2.properties這個檔案的路徑是/config/config2.properties,

所以我們可以使用下面的代碼擷取這兩個檔案——從根下擷取資源

package com.winwill.test;    import org.junit.Test;    import java.io.IOException;  import java.io.InputStream;    /**  * Created by qifuguang on 14-9-28.  */  public class TestGetResourceAsStream {      @Test      public void getResourceClassAndFileInSamePackage() throws IOException {          // 擷取config2.properties          InputStream config2 = this.getClass().getResourceAsStream("/config/config2.properties");            // 擷取config.properties          InputStream config = this.getClass().getResourceAsStream("/config.properties");      }  }  

上面的代碼使用的是從根目錄開始的絕對路徑。

那麼可不可以使用相對路徑呢?當然可以,請看下面的代碼——從類所在的包下擷取資源

package com.winwill.test;    import org.junit.Test;    import java.io.IOException;  import java.io.InputStream;    /**  * Created by qifuguang on 14-9-28.  */  public class TestGetResourceAsStream {      @Test      public void getResourceClassAndFileInSamePackage() throws IOException {          // 擷取config2.properties          InputStream config2 = this.getClass().getResourceAsStream("../../../config/config2.properties");            // 擷取config.properties          InputStream config = this.getClass().getResourceAsStream("../../../config.properties");          System.out.println(config + " " + config2);      }  }  

代碼中的兩個路徑就是這兩個檔案相對TestGetResourceAsStream.class這個檔案的的相對路徑。

1.3 使用getClass().getClassLoader().getResourceAsStream(String path)

ps:預設是從ClassPath根下擷取,path不能以’/‘開頭,最終是由ClassLoader擷取資源。

getClass().getClassLoader().getResourceAsStream()預設使用的路徑就是class檔案的根目錄,所以使用getClass().getClassLoader().getResourceAsStream()來擷取檔案的時候不能在路徑前面加上/,於是我們可以這樣擷取這兩個檔案:

package com.winwill.test;    import org.junit.Test;    import java.io.IOException;  import java.io.InputStream;    /**  * Created by qifuguang on 14-9-28.  */  public class TestGetResourceAsStream {      @Test      public void getResourceClassAndFileInSamePackage() throws IOException {          // 擷取config2.properties          InputStream config2 = this.getClass().getClassLoader().getResourceAsStream("config/config2.properties");            // 擷取config.properties          InputStream config = this.getClass().getClassLoader().getResourceAsStream("config.properties");      }  }  
 1.4  WebAPP根目錄下取資源

 ServletContext. getResourceAsStream(String path):預設從WebAPP根目錄下取資源,Tomcat下path是否以’/‘開頭無所謂,當然這和具體的容器實現有關。

 Jsp下的application內建對象就是上面的ServletContext的一種實現。

 

(轉)java getResourceAsStream的使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.