I am trying to clear the contents of a file I made in java. The file is created by a PrintWriter call. I read here that one can use RandomAccessFile to do so, and read somewhe
You want to either flush the PrintWriter
to make sure the changes in its buffer are written out first, before you set the RandomAccessFile
's length to 0, or close it and re-open a new PrintWriter
to write the last line (Text to be written...). Preferably the former:
if (clearCondition) {
writer.flush();
new RandomAccessFile("temp","rw").setLength(0);