以追加的方式寫入文件


 1 public void appendResult(String content){
 2         
 3         String filePath = System.getProperty("user.dir");
 4         String fileName = "sys_check_info.txt";
 5         BufferedWriter bufferOut = null;
 6         try{
 7             
 8             String fileDir = filePath +"/"+fileName;
 9             File file = new File(fileDir);
10             
11             if(!file.exists()){
12                 file.createNewFile();
13             }
14             //追加方式寫入文件
15             bufferOut = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true),"UTF-8"));            
16             bufferOut.write(content);
17             bufferOut.newLine();
18         }catch(Exception e){
19             
20         }finally{
21             try {     
22                 if(bufferOut != null){  
23                     bufferOut.close();     
24                 }  
25             } catch (IOException e) {     
26                 e.printStackTrace();     
27             }             
28         }
29     }
public static void main(String[] args) {
        GenerateResult gr = new GenerateResult();
        String cont1 = "I like you!";
        String cont2 = "I love you!";
        String cont3 = "I want you!";
        gr.appendResult(cont1);
        gr.appendResult(cont2);
        gr.appendResult(cont3);        
    }
文件內容:
I like you!
I love you!
I want you!

注意!

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



 
  © 2014-2022 ITdaan.com 联系我们: