yangno 2017-03-23
//向文本文件中追加内容,如果append=true则默认追加,如果append=false会先清空文件再追加内容 public static boolean updateContent(String fileName,String content,boolean append){ boolean res = true; File file = new File(fileName); try { if (!file.exists()) { file.createNewFile(); } FileWriter writer = new FileWriter(file,append); if(append){ content = System.getProperty("line.separator")+content; System.out.println(content); } writer.write(content); writer.flush(); writer.close(); } catch (IOException ex) { res = false; ex.printStackTrace(); } return res; }
特别要注意的是FileWriter只有一个参数的构造函数,缺省会将原来的文件清空!
FileWriterfw=newFileWriter(file);