Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          java路徑問題總結(轉載)

          一 相對路徑的獲得
          ??? 說明:相對路徑(即不寫明時候到底相對誰)均可通過以下方式獲得(不論是一般的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

          posted on 2010-01-15 13:49 Rabbit 閱讀(215) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 南平市| 武邑县| 三明市| 申扎县| 开远市| 延寿县| 岑巩县| 海林市| 灵宝市| 宁陕县| 天水市| 永吉县| 长沙县| 遂溪县| 古交市| 唐海县| 凌海市| 高清| 安多县| 施秉县| 德州市| 安阳市| 平潭县| 邮箱| 桂阳县| 盱眙县| 互助| 呼图壁县| 台东市| 海南省| 达拉特旗| 娄烦县| 赤壁市| 女性| 荔浦县| 葵青区| 永康市| 闽清县| 阳朔县| 四会市| 中宁县|