文件分隔符

chyx413332087 13年前發布 | 2K 次閱讀
  "/"與"\"還有"\\"  
 / 是unix,或者linux系統下得文件路徑表示
\:是windox系統下得
\\:  這個在window下,用在java中相當于  \\==/    \\代表 \代表轉義字符
 
標準的用法是 '/ ',   在具體的FileSystem上,會把這個符號替換成本地的separator,  
而直接寫 '\ '只可能用在windows系統下
File.separator:   String   "/ "(unix)   or   "\ "(windows)
File.separatorChar   :   同上   char   '/ ',   '\ '
File.pathSeparator   :   String,   一系列path之間的分隔符,   ": "unix   "; "windows
File.pathSeparatorChar
 
還有  .  和  ..
.  代表當前目錄
..  代表當前目錄的上級目錄
舉例:
在F:/A/B/C下
那么 ../C就是 F:/A/B
那么 ../B就是 F:/A
./C或者C就是 F:/A/B/C
/C 是指 F:/C
 

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

 

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

    
 
 
 
 

 
 本文由用戶 chyx413332087 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!