waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實實做事,認認真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
          1. import java.io.BufferedWriter;   
          2. import java.io.FileOutputStream;   
          3. import java.io.FileWriter;   
          4. import java.io.IOException;   
          5. import java.io.OutputStreamWriter;   
          6. import java.io.RandomAccessFile;   
          7.   
          8. /**  
          9.  * 描述:追加內容到文件末尾  
          10.  * @author Administrator  
          11.  *  
          12.  */  
          13. public class WriteStreamAppend {   
          14.     /**  
          15.      * 追加文件:使用FileOutputStream,在構造FileOutputStream時,把第二個參數設為true  
          16.      *   
          17.      * @param fileName  
          18.      * @param content  
          19.      */  
          20.     public static void method1(String file, String conent) {   
          21.         BufferedWriter out = null;   
          22.         try {   
          23.             out = new BufferedWriter(new OutputStreamWriter(   
          24.                     new FileOutputStream(file, true)));   
          25.             out.write(conent);   
          26.         } catch (Exception e) {   
          27.             e.printStackTrace();   
          28.         } finally {   
          29.             try {   
          30.                 out.close();   
          31.             } catch (IOException e) {   
          32.                 e.printStackTrace();   
          33.             }   
          34.         }   
          35.     }   
          36.   
          37.     /**  
          38.      * 追加文件:使用FileWriter  
          39.      *   
          40.      * @param fileName  
          41.      * @param content  
          42.      */  
          43.     public static void method2(String fileName, String content) {   
          44.         try {   
          45.             // 打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件   
          46.             FileWriter writer = new FileWriter(fileName, true);   
          47.             writer.write(content);   
          48.             writer.close();   
          49.         } catch (IOException e) {   
          50.             e.printStackTrace();   
          51.         }   
          52.     }   
          53.   
          54.     /**  
          55.      * 追加文件:使用RandomAccessFile  
          56.      *   
          57.      * @param fileName  
          58.      *            文件名  
          59.      * @param content  
          60.      *            追加的內容  
          61.      */  
          62.     public static void method3(String fileName, String content) {   
          63.         try {   
          64.             // 打開一個隨機訪問文件流,按讀寫方式   
          65.             RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");   
          66.             // 文件長度,字節數   
          67.             long fileLength = randomFile.length();   
          68.             // 將寫文件指針移到文件尾。   
          69.             randomFile.seek(fileLength);   
          70.             randomFile.writeBytes(content);   
          71.             randomFile.close();   
          72.         } catch (IOException e) {   
          73.             e.printStackTrace();   
          74.         }   
          75.     }   
          76.   
          77.     public static void main(String[] args) {   
          78.         System.out.println("start");   
          79.         method1("c:/test.txt""追加到文件的末尾");   
          80.         System.out.println("end");   
          81.     }   
          82.   
          83. }  
          posted on 2009-04-15 21:59 weesun一米陽光 閱讀(337) 評論(0)  編輯  收藏 所屬分類: 總結備用
          主站蜘蛛池模板: 静乐县| 平罗县| 景谷| 鱼台县| 曲麻莱县| 海晏县| 南通市| 松桃| 兴山县| 阜康市| 内黄县| 施甸县| 太仓市| 吉林省| 龙山县| 自治县| 罗平县| 韶关市| 湖北省| 怀集县| 顺昌县| 敦化市| 唐河县| 曲水县| 大新县| 济源市| 高安市| 上饶县| 图片| 天全县| 桑植县| 阜阳市| 民丰县| 保靖县| 葫芦岛市| 浏阳市| 辽宁省| 神农架林区| 砀山县| 都匀市| 航空|