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!