java - How to empty file content and then append text multiple times -
i have file (file.txt), , need empty current content, , append text multiple times.
example: file.txt current content is:
aaa
bbb
ccc
i want remove content, , append first time:
ddd
the second time:
eee
and on...
i tried this:
// empty current content fileout = new filewriter("file.txt"); fileout.write(""); fileout.close(); // append fileout = new filewriter("file.txt", true); // when want write multiple times: fileout.write("text"); fileout.flush();
this works fine, seems inefficient because open file 2 times remove current content.
when open file write new text, overwrite whatever in file already.
a way is
// empty current content fileout = new filewriter("file.txt"); fileout.write(""); fileout.append("all text"); fileout.close();
Comments
Post a Comment