Java通过BufferedWriter追加内容到文件末尾的几种常用方法

io操作过程,有时候,我们是需要重新创建文件,有时候只要在文件末尾追加内容,就可以,那么如何追加呢,给出3种方法:

  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. public class WriteStreamAppend {                                                     
  10.                                                                                      
  11.     public static void method1(String file, String conent) {                         
  12.         BufferedWriter out = null;                                                   
  13.         try {                                                                        
  14.              //FileOutputStream构造函数中的第二个参数true表示以追加形式写文件 
  15.              out = new BufferedWriter(new OutputStreamWriter(                        
  16.                     new FileOutputStream(file, true)));                              
  17.              out.write(conent);                                                      
  18.          } catch (Exception e) {                                                     
  19.              e.printStackTrace();                                                    
  20.          } finally {                                                                 
  21.             try {                                                                    
  22.                  out.close();                                                        
  23.              } catch (IOException e) {                                               
  24.                  e.printStackTrace();                                                
  25.              }                                                                       
  26.          }                                                                           
  27.      }                                                                               
  28.                                                                                      
  29.                                                                                      
  30.     public static void method2(String fileName, String content) {                    
  31.         try {                                                                        
  32.             // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件      
  33.              FileWriter writer = new FileWriter(fileName, true);                     
  34.              writer.write(content);                                                  
  35.              writer.close();                                                         
  36.          } catch (IOException e) {                                                   
  37.              e.printStackTrace();                                                    
  38.          }                                                                           
  39.      }                                                                               
  40.                                                                                      
  41.                                                                                      
  42.     public static void method3(String fileName, String content) {                    
  43.         try {                                                                        
  44.             // 打开一个随机访问文件流,按读写方式                                    
  45.              RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");     
  46.             // 文件长度,字节数                                                      
  47.             long fileLength = randomFile.length();                                   
  48.             // 将写文件指针移到文件尾。                                              
  49.              randomFile.seek(fileLength);                                            
  50.              randomFile.writeBytes(content);                                         
  51.              randomFile.close();                                                     
  52.          } catch (IOException e) {                                                   
  53.              e.printStackTrace();                                                    
  54.          }                                                                           
  55.      }                                                                               
  56.                                                                                      
  57.     public static void main(String[] args) {                                         
  58.          System.out.println("start");                                                
  59.          method1("c:/test.txt""追加到文件的末尾");                                 
  60.          System.out.println("end");                                                  
  61.      }
  62.                    


参考文章:https://blog.csdn.net/stefshawn/article/details/7303124



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值