java路徑問題總結

來源:互聯網
上載者:User

平時寫程式的時候,很多時候提示檔案找不到,而拋出了異常,現在整理如下

 

一 相對路徑的獲得
    說明:相對路徑(即不寫明時候到底相對誰)均可通過以下方式獲得(不論是一般的java項目還是web項目)
         String relativelyPath=System.getProperty("user.dir");
         上述相對路徑中,java項目中的檔案是相對於項目的根目錄
         web項目中的檔案路徑視不同的web伺服器不同而不同(tomcat是相對於 tomcat安裝目錄/bin)

二 類載入目錄的獲得(即當運行時某一類時獲得其裝載目錄)
      1.1)通用的方法一(不論是一般的java項目還是web項目,先定位到能看到包路徑的第一級目錄)
       
        InputStream is=TestAction.class.getClassLoader().getResourceAsStream("test.txt");
                  (test.txt檔案的路徑為  項目名/src/test.txt;類TestAction所在包的第一級目錄位於src目錄下)
       
        上式中將TestAction,test.txt替換成對應成相應的類名和檔案名稱字即可

        1.2)通用方法二  (此方法和1.1中的方法類似,不同的是此方法必須以'/'開頭)
             InputStream is=Test1.class.getResourceAsStream("/test.txt");
                      (test.txt檔案的路徑為  項目名/src/test.txt,類Test1所在包的第一級目錄位於src目錄下)

     

三 web項目根目錄的獲得(發布之後)
    1 從servlet出發

    可建立一個servlet在其的init方法中寫入如下語句
      ServletContext s1=this.getServletContext();
     String temp=s1.getRealPath("/"); (關鍵)
     結果形如:D:/工具/Tomcat-6.0/webapps/002_ext/  (002_ext為項目名字)

                     如果是調用了s1.getRealPath("")則輸出D:/工具/Tomcat-6.0/webapps/002_ext(少了一個"/")

   2 從httpServletRequest出發

             String cp11111=request.getSession().getServletContext().getRealPath("/");

      結果形如:D:/工具/Tomcat-6.0/webapps/002_ext/

 

 

四  classpath的擷取(在Eclipse中為獲得src或者classes目錄的路徑)

    方法一   Thread.currentThread().getContextClassLoader().getResource("").getPath()

 

                      eg: String t=Thread.currentThread().getContextClassLoader().getResource("").getPath();
                      System.out.println("t---"+t);

                      輸出:t---/E:/order/002_ext/WebRoot/WEB-INF/classes/

 

 

 

   方法二      JdomParse.class.getClassLoader().getResource("").getPath()   (JdomParse為src某一個包中的類,下同)

                 eg:String p1=JdomParse.class.getClassLoader().getResource("").getPath();
                     System.out.println("JdomParse.class.getClassLoader().getResource--"+p1);

   輸出: JdomParse.class.getClassLoader().getResource--/E:/order/002_ext/WebRoot/WEB-INF/classes/

 

另外,如果想把檔案放在某一包中,則可以 通過以下方式獲得到檔案(先定位到該包的最後一級目錄)

        eg  String p2=JdomParse.class.getResource("").getPath();
         System.out.println("JdomParse.class.getResource---"+p2);

   輸出: JdomParse.class.getResource---/E:/order/002_ext/WebRoot/WEB-INF/classes/jdom/ (JdomParse為src目錄下jdom包中的類)

 

四   屬性檔案的讀取:

     方法 一

           InputStream in = lnew BufferedInputStream( new  FileInputStream(name));          Properties p = new  Properties();    p.load(in); 

 

    注意路徑的問題,做執行之後就可以調用p.getProperty("name")得到對應屬性的值

 

方法二

        Locale locale = Locale.getDefault(); 
              ResourceBundle localResource = ResourceBundle.getBundle("test/propertiesTest", locale); 
            String value = localResource.getString("test"); 
           System.out.println("ResourceBundle: " + value);

     工程src目錄下propertiesTest.properties(名字尾碼必須為properties)檔案內容如下:

               test=hello word

聯繫我們

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