Cyh的博客

          Email:kissyan4916@163.com
          posts - 26, comments - 19, trackbacks - 0, articles - 220

          添加內容到文件尾

          Posted on 2009-05-24 21:26 啥都寫點 閱讀(567) 評論(0)  編輯  收藏 所屬分類: J2SE
          關鍵技術:
          • 通過RandomAccessFile以讀寫的方式打開文件輸出流,使用它的seek方法可以將讀寫指針移到文件尾,再使用它的write方法將數據寫到讀寫指針后面,完成文件的追加
          • 通過FileWriter打開文件輸出流,構造FileWriter時指定寫入模式,是一個布爾值,為true時表示寫入的內容添加到已有文件內容的后面,為false時重新寫文件,以前的數據被清空,默認為fasle。

          package book.io;

          import java.io.FileWriter;
          import java.io.IOException;
          import java.io.RandomAccessFile;

          /**
           * 將內容追加到文件尾部
           
          */
          public class AppendToFile {

              
          /**
               * A方法追加文件:使用RandomAccessFile
               * 
          @param fileName    文件名
               * 
          @param content    追加的內容
               
          */
              
          public static void appendMethodA(String fileName, String content){
                  
          try {
                      
          //    打開一個隨機訪問文件流,按讀寫方式
                      RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
                      
          //    文件長度,字節數
                      long fileLength = randomFile.length();
                      
          //將寫文件指針移到文件尾。
                      randomFile.seek(fileLength);
                      randomFile.writeBytes(content);
                      randomFile.close();
                  } 
          catch (IOException e){
                      e.printStackTrace();
                  }
              }
              
          /**
               * B方法追加文件:使用FileWriter
               * 
          @param fileName
               * 
          @param content
               
          */
              
          public static void appendMethodB(String fileName, String content){
                  
          try {
                      
          //打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件
                      FileWriter writer = new FileWriter(fileName, true);
                      writer.write(content);
                      writer.close();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
              }

              
          public static void main(String[] args) {
                  String fileName 
          = "C:/temp/newTemp.txt";
                  String content 
          = "new append!";
                  
          //按方法A追加文件
                  AppendToFile.appendMethodA(fileName, content);
                  AppendToFile.appendMethodA(fileName, 
          "append end. \n");
                  
          //顯示文件內容
                  ReadFromFile.readFileByLines(fileName);
                  
          //按方法B追加文件
                  AppendToFile.appendMethodB(fileName, content);
                  AppendToFile.appendMethodB(fileName, 
          "append end. \n");
                  
          //顯示文件內容
                  ReadFromFile.readFileByLines(fileName);
              }
          }



                                                                                                                 --    學海無涯
                  

          主站蜘蛛池模板: 天等县| 合水县| 克拉玛依市| 南和县| 天长市| 乐至县| 石渠县| 清丰县| 和硕县| 应城市| 泽库县| 蛟河市| 永安市| 肥东县| 怀宁县| 民县| 泗洪县| 平顶山市| 渝中区| 甘泉县| 江陵县| 信宜市| 浮梁县| 黔西| 平塘县| 利川市| 江源县| 甘肃省| 辰溪县| 开封县| 游戏| 新宾| 祥云县| 获嘉县| 宝兴县| 葫芦岛市| 新兴县| 天峻县| 菏泽市| 桐梓县| 鄂尔多斯市|