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



Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐