向文本文件追加内容,清空文件内容
//向文本文件中追加内容,如果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);
相关推荐
Roka 2020-06-28
xhpscdx 2020-05-17
chaigang 2020-02-25
歆萌 2020-02-02
chunianyo 2019-12-21
疯狂紫萧 2019-12-14
PHP淮北PHP00网 2013-09-17
QCkiss 2019-11-04
歆萌 2019-11-03
Kwong 2019-10-22
宇智波鼬 2018-08-30
zhrookie 2011-11-18
81423067 2015-06-02
wewoor 2015-12-27
lizaochengwen 2019-03-19
stSahana 2016-11-01