waysun一路陽光

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            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.  * 描述:追加內(nèi)容到文件末尾  
          10.  * @author Administrator  
          11.  *  
          12.  */  
          13. public class WriteStreamAppend {   
          14.     /**  
          15.      * 追加文件:使用FileOutputStream,在構(gòu)造FileOutputStream時,把第二個參數(shù)設(shè)為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.             // 打開一個寫文件器,構(gòu)造函數(shù)中的第二個參數(shù)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.      *            追加的內(nèi)容  
          61.      */  
          62.     public static void method3(String fileName, String content) {   
          63.         try {   
          64.             // 打開一個隨機訪問文件流,按讀寫方式   
          65.             RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");   
          66.             // 文件長度,字節(jié)數(shù)   
          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一米陽光 閱讀(345) 評論(0)  編輯  收藏 所屬分類: 總結(jié)備用
          主站蜘蛛池模板: 临沂市| 射阳县| 浦江县| 罗田县| 新泰市| 广宗县| 太原市| 温泉县| 四平市| 栾川县| 大足县| 台南市| 凉城县| 偃师市| 叙永县| 来宾市| 阳江市| 治县。| 台前县| 连州市| 金乡县| 沙坪坝区| 榆林市| 吴川市| 浑源县| 乌什县| 金门县| 淳化县| 郁南县| 武邑县| 荔波县| 淮南市| 八宿县| 东海县| 九龙县| 富平县| 合水县| 海城市| 临邑县| 共和县| 客服|