waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實(shí)實(shí)做事,認(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í),把第二個(gè)參數(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è)寫文件器,構(gòu)造函數(shù)中的第二個(gè)參數(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.             // 打開一個(gè)隨機(jī)訪問文件流,按讀寫方式   
          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一米陽光 閱讀(337) 評論(0)  編輯  收藏 所屬分類: 總結(jié)備用
          主站蜘蛛池模板: 安庆市| 绵竹市| 肇庆市| 衡阳县| 辽阳县| 桓仁| 玛曲县| 文山县| 景宁| 镇坪县| 湘西| 大足县| 乡城县| 汝南县| 财经| 阿克陶县| 平顶山市| 海门市| 房产| 鞍山市| 江川县| 梅河口市| 盈江县| 晋城| 永寿县| 宁晋县| 集贤县| 襄樊市| 罗山县| 潞西市| 正安县| 习水县| 邵阳县| 陕西省| 琼结县| 舟山市| 侯马市| 大名县| 恩平市| 建宁县| 克什克腾旗|