廉頗老矣,尚能飯否

          java:從技術到管理

          常用鏈接

          統計

          最新評論

          java讀取外部文件【轉載】

           (1)File file = new File(root );
             if (file.exists()) {
              File[] filesArray = file.listFiles();
              if (filesArray != null) {
               for (File f : filesArray) {
                if (f.isFile()) {
                 BufferedReader in = new BufferedReader(new FileReader(f.getPath()));
                       String msg = in.readLine();
                      System.out.println(msg);
                       break;
                }
               }
              }
             }
          上面是代碼完成的功能是檢查一個目錄下是否有文件,如果有,把讀取到的第一個文件的內容讀出來。 
          與之相反的處理:
            (2) File file = new File("C:\\CSSDG\\BatchError\\");
             if (file.exists()) {
              File[] filesArray = file.listFiles();
              if (filesArray != null) {
               for (File f : filesArray) {
                if (f.isFile()) {
                 BufferedWriter out = new BufferedWriter(new FileWriter(f.getPath()));
                       out.write("TEST成功!");
                       out.close();
                }
               }
              }
             }
          注意,(2)和(1)有兩點不同。
          一是(1)中Reader的地方都要改成Writer,另外,(2)是對外部文件進行更新,要有一個斷開連接的過程,就是close方法。

          柳德才
          13691193654
          18942949207
          QQ:422157370
          liudecai_zan@126.com
          湖北-武漢-江夏-廟山

          posted on 2009-01-19 23:36 liudecai_zan@126.com 閱讀(523) 評論(0)  編輯  收藏 所屬分類: 在路上

          主站蜘蛛池模板: 阿拉善盟| 峡江县| 尚义县| 运城市| 兴城市| 揭阳市| 左权县| 金湖县| 祁东县| 万州区| 乳山市| 鱼台县| 武功县| 工布江达县| 瑞安市| 金溪县| 黄石市| 建宁县| 佛山市| 临沂市| 丽水市| 霸州市| 西贡区| 常宁市| 永修县| 温宿县| 连平县| 三门县| 巧家县| 乌拉特中旗| 西吉县| 赣州市| 璧山县| 营口市| 大冶市| 信宜市| 尖扎县| 微博| 彰武县| 三明市| 长垣县|