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

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

你可能感兴趣的:(JavaSE)