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)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 云林县| 鱼台县| 江油市| 普兰店市| 乐山市| 乌鲁木齐县| 韩城市| 靖西县| 玉林市| 嘉善县| 崇左市| 上虞市| 讷河市| 静乐县| 宣化县| 铅山县| 松溪县| 灵璧县| 砀山县| 克什克腾旗| 新兴县| 通州市| 九台市| 清水河县| 德清县| 宝山区| 互助| 林州市| 军事| 临澧县| 灯塔市| 名山县| 禹城市| 绵竹市| 泰州市| 商水县| 奇台县| 凤翔县| 元朗区| 勃利县| 托里县|