I'm building a program that constantly writes to the same file, using java.io.FileWriter
.
During run-time, whenever I need to update the file, I call a method that writes "properly" and uses a class-level FileWriter. The problem starts on the second .write
call, when the FileWriter starts appending to the file, instead of overwriting it.
public class Example {
private FileWriter fw = new FileWriter("file's path", false);
public void writeToFile(String output) {
fw.write(output);
fw.flush();
}
}
I'm suspecting that somehow the FileWriter's "memory" keeps its previously written data, but I don't want to close and initialize it for every call, as it seems wasteful.
- Do I even need the
.flush
call after each.write
call? - Do I have another option but initializing the FileWriter and
.close
-ing it on every call?
(I tried looking in older questions, but they all seem to deal with FileWriters that won't append, or theoretical FileWriter.flush
interpretations)