Java讀文件寫文件操作(續)

           1 package velcro.util;  
           2   
           3 import java.io.File;  
           4 import java.io.FileWriter;  
           5 import java.io.IOException;  
           6   
           7 /** 
           8  * 對文本文件進行讀寫操作 
           9  */  
          10 public class WriteAndReadText {  
          11     /** 
          12      * 文本文件所在的目錄 
          13      */  
          14     private String textPath;  
          15     /** 
          16      * 讀取文本內容 
          17      * @param textname 文本名稱 
          18      * @return 
          19      */  
          20     public String readText(String textname){  
          21         File file=new File(textPath+File.separator+textname);  
          22         try {  
          23             BufferedReader br = new BufferedReader(new java.io.FileReader(file));  
          24             StringBuffer sb = new StringBuffer();  
          25             String line = br.readLine();  
          26             while (line != null) {  
          27                 sb.append(line);  
          28                 line = br.readLine();  
          29             }  
          30             br.close();  
          31             return sb.toString();  
          32         } catch (IOException e) {  
          33             LogInfo.error(this.getClass().getName(),e.getLocalizedMessage(),e);  
          34             e.printStackTrace();  
          35             return null;  
          36         }  
          37     }  
          38     }  
          39     /** 
          40      * 將內容寫到文本中 
          41      * @param textname 文本名稱 
          42      * @param date 寫入的內容 
          43      * @return 
          44      */  
          45     public boolean writeText(String textname,String date){  
          46         boolean flag=false;  
          47         File filePath=new File(textPath);  
          48         if(!filePath.exists()){  
          49             filePath.mkdirs();  
          50         }  
          51         try {  
          52             FileWriter fw =new FileWriter(textPath+File.separator+textname);  
          53             fw.write(date);  
          54             flag=true;  
          55             if(fw!=null)  
          56                 fw.close();  
          57         } catch (IOException e) {  
          58             LogInfo.error(this.getClass().getName(),e.getMessage(),e);  
          59             e.printStackTrace();  
          60         }  
          61   
          62         return flag;          
          63     }  
          64     /** 
          65      * 在文檔后附加內容 
          66      * @param textName 
          67      * @param date 
          68      * @return 
          69      */  
          70     public boolean appendText(String textName,String date){  
          71         boolean flag=false;  
          72         File filePath=new File(textPath);  
          73         if(!filePath.exists()){  
          74             filePath.mkdirs();  
          75         }  
          76         try {  
          77             FileWriter fw =new FileWriter(textPath+File.separator+textName,true);  
          78             fw.append(date);  
          79             flag=true;  
          80             if(fw!=null)  
          81                 fw.close();  
          82         } catch (IOException e) {  
          83             LogInfo.error(this.getClass().getName(),e.fillInStackTrace().toString());  
          84             e.printStackTrace();  
          85         }  
          86         return flag;      
          87     }  
          88     public String getTextPath() {  
          89         return textPath;  
          90     }  
          91     public void setTextPath(String textPath) {  
          92         this.textPath = textPath;  
          93     }  
          94 }  
          95 

          PrintWriter out = new PrintWriter(new FileWriter(logFileName, true), true); 
          Java讀寫文件最常用的類是FileInputStream/FileOutputStreamFileReader/FileWriter。 
          其中FileInputStreamFileOutputStream是基于字節流的,常用于讀寫二進制文件。 
          讀寫字符文件建議使用基于字符的FileReaderFileWriter,省去了字節與字符之間的轉換。 
          但這兩個類的構造函數默認使用系統的編碼方式,如果文件內容與系統編碼方式不一致,可能會出現亂碼。 
          在這種情況下,建議使用FileReaderFileWriter的父類:InputStreamReader/OutputStreamWriter, 
          它們也是基于字符的,但在構造函數中可以指定編碼類型:InputStreamReader(InputStream in, Charset cs) OutputStreamWriter(OutputStream out, Charset cs)。 

          // 讀寫文件的編碼: 
          InputStreamReader r = new InputStreamReader(new FileInputStream(fileName), “utf-8″); 
          OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName),”utf-8″);




          posted on 2009-07-17 22:16 彭偉 閱讀(928) 評論(0)  編輯  收藏 所屬分類: java技術分區

          <2009年7月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 江都市| 广元市| 河南省| 东台市| 九寨沟县| 于田县| 桑日县| 寿光市| 奇台县| 靖远县| 靖安县| 壤塘县| 聂拉木县| 莲花县| 曲水县| 承德市| 余干县| 锡林浩特市| 绥棱县| 旌德县| 吉林省| 海安县| 桦甸市| 伊宁县| 皮山县| 老河口市| 三亚市| 通城县| 商河县| 多伦县| 施甸县| 鲁山县| 香港 | 奈曼旗| 景德镇市| 满洲里市| 北碚区| 靖州| 平阴县| 团风县| 南溪县|