Java相對路徑讀取檔案

來源:互聯網
上載者:User
不管你是新手還是老鳥,在程式中讀取資源檔總會遇到一些找不到檔案的問題,這與Java底層的實現有關,不能算bug,只要方法得當,問題還是可以解決的。 項目的檔案夾結構:repathtest 
├─src 
│    └─com 
│            └─lavasoft 
│                    ├─test 
│                    └─res 
├─doc    1、在Java開發工具的project中使用相對路徑在project中,相對路徑的根目錄是project的根資料夾,在此就是repathtest檔案夾了。建立檔案的寫法是:File f = new File("src/com/lavasoft/res/a.txt"); File f = new File("doc/b.txt"); 注意:路徑不以“/”開頭;脫離了IDE環境,這個寫法就是錯誤的,也並非每個IDE都如此,但我見到的都是這樣的。  2、通過CLASSPATH讀取包內檔案讀取包內檔案,使用的路徑一定是相對的classpath路徑,比如a,位於包內,此時可以建立讀取a的位元組流:InputStream in = ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt");有了位元組流,就能讀取到檔案內容了。 注意:這裡必須以“/”開頭;  3、看看完整的測試代碼package com.lavasoft.test; 

import java.io.*; 

/** 
* Java讀取相對路徑的檔案 

* @author leizhimin 2010-1-15 10:59:43 
*/ 
public class ReadFile { 
        public static void main(String[] args) { 
                readTextA_ByClassPath(); 
                readTextA_ByProjectRelativePath(); 
                readTextB_ByProjectRelativePath(); 
        } 

        /** 
         * 通過工程相對路徑讀取(包內)檔案,注意不以“/”開頭 
         */ 
        public static void readTextA_ByProjectRelativePath() { 
                System.out.println("-----------------readTextA_ByProjectRelativePath---------------------"); 
                File f = new File("src/com/lavasoft/res/a.txt"); 
                String a = file2String(f, "GBK"); 
                System.out.println(a); 
        } 

        /** 
         * 通過工程相對路徑讀取(包外)檔案,注意不以“/”開頭 
         */ 
        public static void readTextB_ByProjectRelativePath() { 
                System.out.println("-----------------readTextB_ByProjectRelativePath---------------------"); 
                File f = new File("doc/b.txt"); 
                String b = file2String(f, "GBK"); 
                System.out.println(b); 
        } 

        /** 
         * 通過CLASSPATH讀取包內檔案,注意以“/”開頭 
         */ 
        public static void readTextA_ByClassPath() { 
                System.out.println("-----------------readTextA_ByClassPath---------------------"); 
                InputStream in = ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt"); 
                String a = stream2String(in, "GBK"); 
                System.out.println(a); 
        } 

        /** 
         * 檔案轉換為字串 
         * 
         * @param f             檔案 
         * @param charset 檔案的字元集 
         * @return 檔案內容 
         */ 
        public static String file2String(File f, String charset) { 
                String result = null; 
                try { 
                        result = stream2String(new FileInputStream(f), charset); 
                } catch (FileNotFoundException e) { 
                        e.printStackTrace(); 
                } 
                return result; 
        } 

        /** 
         * 檔案轉換為字串 
         * 
         * @param in            位元組流 
         * @param charset 檔案的字元集 
         * @return 檔案內容 
         */ 
        public static String stream2String(InputStream in, String charset) { 
                StringBuffer sb = new StringBuffer(); 
                try { 
                        Reader r = new InputStreamReader(in, charset); 
                        int length = 0; 
                        for (char[] c = new char[1024]; (length = r.read(c)) != -1;) { 
                                sb.append(c, 0, length); 
                        } 
                        r.close(); 
                } catch (UnsupportedEncodingException e) { 
                        e.printStackTrace(); 
                } catch (FileNotFoundException e) { 
                        e.printStackTrace(); 
                } catch (IOException e) { 
                        e.printStackTrace(); 
                } 
                return sb.toString(); 
        } 
}(代碼寫得粗糙,異常沒做認真處理) 運行結果:-----------------readTextA_ByClassPath--------------------- 
aaaaaaaaa 
sssssssss 
-----------------readTextA_ByProjectRelativePath--------------------- 
aaaaaaaaa 
sssssssss 
-----------------readTextB_ByProjectRelativePath--------------------- 
bbbbbbbbbbb 

Process finished with exit code 0

 這是通過IDEA開發工具啟動並執行,結果沒問題,如果換成控制台執行,那麼使用了項目相對路徑的讀取方式會失敗,原因是,此時已經脫離了項目的開發環境,-----這個問題常常困擾著一些菜鳥,代碼在開發工具好好的,發布後執行就失敗了!下面我截個圖:  5、擷取CLASSPATH下檔案的絕對路徑當使用相對路徑寫入檔案時候,就需要用到絕對路徑。下面是個例子:package com.lavasoft; 

import java.io.File; 

/** 
* CLASSPATH檔案的絕對路徑擷取測試 

* @author leizhimin 2010-1-18 9:33:02 
*/ 
public class Test { 
        //classpath的檔案路徑 
        private static String cp = "/com/lavasoft/cfg/syscfg.properties"; 

        public static void main(String[] args) { 
                //當前類的絕對路徑 
                System.out.println(Test.class.getResource("/").getFile()); 
                //指定CLASSPATH檔案的絕對路徑 
                System.out.println(Test.class.getResource(cp).getFile()); 
                //指定CLASSPATH檔案的絕對路徑 
                File f = new File(Test.class.getResource(cp).getFile()); 
                System.out.println(f.getPath()); 
        } 
}  輸出:/D:/projects/bbt/code/cdn/planrpt/out/production/planrpt/ 
/D:/projects/bbt/code/cdn/planrpt/out/production/planrpt/com/lavasoft/cfg/syscfg.properties 
D:/projects/bbt/code/cdn/planrpt/out/production/planrpt/com/lavasoft/cfg/syscfg.properties 

Process finished with exit code 0

  總結使用工程相對路徑是靠不住的。使用CLASSPATH路徑是可靠的。對於程式要讀取的檔案,儘可能放到CLASSPATH下,這樣就能保證在開發和發布時候均正常讀取。原文:http://lavasoft.blog.51cto.com/62575/265821

聯繫我們

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