從制造到創造
          軟件工程師成長之路
          posts - 292,  comments - 96,  trackbacks - 0

                  某些時候,我們希望讀寫某個包下面的文件,例如包com.coderdream.chapter20.flyweight.sample下的txt文件,但我們并不想把文件寫成絕對路徑。
          解決辦法:
                  1、我們可以通過新建一個空文件,然后的到當前文件的絕對路徑;
                  2、然后用這個路徑+文件夾名來新建一個BufferedReader。
                  這樣就可以順利讀取包中的文件了。

           1     /**
           2      * 構造函數
           3      * 
           4      * @param charname
           5      */
           6     public BigChar(char charname) {
           7         this.charname = charname;
           8         try {
           9             File file = new File("");
          10             String s =
           file.getAbsolutePath();
          11             BufferedReader reader = new BufferedReader(new
           FileReader(s
          12                     + "\\src\\com\\coderdream\\chapter20"

          13                     + "\\flyweight\\sample\\big" + charname + ".txt"));
          14             String line;
          15             StringBuffer buf = new StringBuffer();
          16             while ((line = reader.readLine()) != null) {
          17                 buf.append(line);
          18                 buf.append("\n");
          19             }
          20 
          21             reader.close();
          22             this.fontdata = buf.toString();
          23         } catch (IOException e) {
          24             this.fontdata = charname + "?";
          25         }
          26     }
          posted on 2007-10-31 17:36 CoderDream 閱讀(640) 評論(0)  編輯  收藏 所屬分類: Java-15.IO

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


          網站導航:
           

          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(9)

          我參與的團隊

          隨筆分類(245)

          隨筆檔案(239)

          文章分類(3)

          文章檔案(3)

          收藏夾(576)

          友情鏈接

          搜索

          •  

          積分與排名

          • 積分 - 458373
          • 排名 - 114

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 逊克县| 黑山县| 双鸭山市| 永修县| 西青区| 满城县| 江山市| 江川县| 和田市| 麻阳| 镇沅| 仁化县| 象山县| 广西| 防城港市| 田阳县| 澄迈县| 汉沽区| 永定县| 河曲县| 宜宾市| 闵行区| 丰顺县| 平罗县| 英山县| 赤城县| 尉氏县| 大渡口区| 淮北市| 茶陵县| 琼中| 滦南县| 余姚市| 林口县| 石台县| 三河市| 拜泉县| 万荣县| 深水埗区| 阳春市| 枣庄市|