java 修改文件的多行内容

1、将文件内容一行一行的读出来 
2、在每读一行的时候,判断是否以a或b开始,如果是则进行处理,然后写到缓冲对象。如果不是则直接写入缓冲对象中 
3、将缓冲对象中的内容回写到文件中 

  1. import java.io.BufferedReader;  
  2. import java.io.BufferedWriter;  
  3. import java.io.FileReader;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6.   
  7. /** 
  8.  * 修改文件 
  9.  */  
  10. public class FileModify {  
  11.   
  12.     /** 
  13.      * 读取文件内容 
  14.      *  
  15.      * @param filePath 
  16.      * @return 
  17.      */  
  18.     public String read(String filePath) {  
  19.         BufferedReader br = null;  
  20.         String line = null;  
  21.         StringBuffer buf = new StringBuffer();  
  22.           
  23.         try {  
  24.             // 根据文件路径创建缓冲输入流  
  25.             br = new BufferedReader(new FileReader(filePath));  
  26.               
  27.             // 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中  
  28.             while ((line = br.readLine()) != null) {  
  29.                 // 此处根据实际需要修改某些行的内容  
  30.                 if (line.startsWith("a")) {  
  31.                     buf.append("start with a").append("\n");  
  32.                 }   
  33.                 // 如果不用修改, 则按原来的内容回写  
  34.                 else {  
  35.                     buf.append(line).append("\n");  
  36.                 }
  37.             }  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.         } finally {  
  41.             // 关闭流  
  42.             if (br != null) {  
  43.                 try {  
  44.                     br.close();  
  45.                 } catch (IOException e) {  
  46.                     br = null;  
  47.                 }  
  48.             }  
  49.         }  
  50.           
  51.         return buf.toString();  
  52.     }  
  53.       
  54.     /** 
  55.      * 将内容回写到文件中 
  56.      *  
  57.      * @param filePath 
  58.      * @param content 
  59.      */  
  60.     public void write(String filePath, String content) {  
  61.         BufferedWriter bw = null;  
  62.           
  63.         try {  
  64.             // 根据文件路径创建缓冲输出流  
  65.             bw = new BufferedWriter(new FileWriter(filePath));  
  66.             // 将内容写入文件中  
  67.             bw.write(content);  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.         } finally {  
  71.             // 关闭流  
  72.             if (bw != null) {  
  73.                 try {  
  74.                     bw.close();  
  75.                 } catch (IOException e) {  
  76.                     bw = null;  
  77.                 }  
  78.             }  
  79.         }  
  80.     }  
  81.       
  82.     /** 
  83.      * 主方法 
  84.      */  
  85.     public static void main(String[] args) {  
  86.         String filePath = "C:/file.txt"// 文件路径  
  87.         FileModify obj = new FileModify();  
  88.         obj.write(filePath, obj.read(filePath)); // 读取修改文件  
  89.     }  
  90.   
  91. }  

猜你喜欢

转载自171151394.iteye.com/blog/2177906