java路徑問題(一)——getClass().getResourceAsStream()

來源:互聯網
上載者:User

標籤:

轉自--->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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.