Java修改文件内容


使用java修改文件内容
  1  package fileopt;
  2  import java.io.BufferedReader;
  3  import java.io.BufferedWriter;
  4  import java.io.FileReader;
  5  import java.io.FileWriter;
  6  import java.io.IOException;
  7  import java.io.RandomAccessFile;
  8 
  9  /**
 10   * 修改文件
 11    */
 12  public  class FileModify {
 13 
 14      /**
 15       * 读取文件内容
 16       * 
 17       *  @param  filePath
 18       *  @return
 19        */
 20      public String read(String filePath) {
 21         BufferedReader br =  null;
 22         String line =  null;
 23         StringBuffer buf =  new StringBuffer();
 24         
 25          try {
 26              //  根据文件路径创建缓冲输入流
 27              br =  new BufferedReader( new FileReader(filePath));
 28             
 29              //  循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
 30               while ((line = br.readLine()) !=  null) {
 31                  //  此处根据实际需要修改某些行的内容
 32                   if (line.startsWith("a")) {
 33                     buf.append(line).append(" start with a");
 34                 }
 35                  else  if (line.startsWith("b")) {
 36                     buf.append(line).append(" start with b");
 37                 }
 38                  //  如果不用修改, 则按原来的内容回写
 39                   else {
 40                     buf.append(line);
 41                 }
 42                 buf.append(System.getProperty("line.separator"));
 43             }
 44         }  catch (Exception e) {
 45             e.printStackTrace();
 46         }  finally {
 47              //  关闭流
 48               if (br !=  null) {
 49                  try {
 50                     br.close();
 51                 }  catch (IOException e) {
 52                     br =  null;
 53                 }
 54             }
 55         }
 56         
 57          return buf.toString();
 58     }
 59     
 60      /**
 61       * 将内容回写到文件中
 62       * 
 63       *  @param  filePath
 64       *  @param  content
 65        */
 66      public  void write(String filePath, String content) {
 67         BufferedWriter bw =  null;
 68         
 69          try {
 70              //  根据文件路径创建缓冲输出流
 71              bw =  new BufferedWriter( new FileWriter(filePath));
 72              //  将内容写入文件中
 73              bw.write(content);
 74         }  catch (Exception e) {
 75             e.printStackTrace();
 76         }  finally {
 77              //  关闭流
 78               if (bw !=  null) {
 79                  try {
 80                     bw.close();
 81                 }  catch (IOException e) {
 82                     bw =  null;
 83                 }
 84             }
 85         }
 86     }
 87      public  void fileAppender(String fileName,String content)  throws IOException{
 88         
 89         BufferedReader reader =  new BufferedReader( new FileReader(fileName));
 90         String line =  null;
 91          //  一行一行的读
 92          StringBuilder sb =  new StringBuilder();
 93         sb.append(content);
 94          while ((line = reader.readLine()) !=  null) {
 95             sb.append(line);
 96             sb.append("\r\n");
 97         }
 98         reader.close();
 99 
100          // 写回去
101          RandomAccessFile mm =  new RandomAccessFile(fileName, "rw");
102         mm.writeBytes(sb.toString());
103         mm.close();
104     }
105     
106      /**
107       * 主方法
108        */
109      public  static  void main(String[] args) {
110         String filePath = FileModify. class.getResource("").getPath()+"test.properties";  //  文件路径
111          FileModify obj =  new FileModify();
112         obj.write(filePath, obj.read(filePath));  //  读取修改文件
113           try {
114             obj.fileAppender(filePath, "set a=b \n");
115         }  catch (IOException e) {
116             e.printStackTrace();
117         }
118     }
119 
120 }


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM