Question

In case I have the following code:

private PrintWriter m_Writer;
m_Writer = new PrintWriter(new FileWriter(k_LoginHistoryFile));

I am writing to a local file on server which name is k_LoginHistoryFile.
Now, as my program runs it doing writings to this file so how can I delete all the file content between each writes?
I think it is important as I don't want to write to a file which will eventually have current updated information on its beginning + not up to date info at its end.

Thanks in advance

Was it helpful?

Solution

This expression:

new FileWriter(k_LoginHistoryFile)

will truncate your file if it exists. It won't just overwrite the start of the file. It's not clear how often this code is executing, but each time it does execute, you'll start a new file (and effectively delete the old contents).

OTHER TIPS

I think it is important as I don't want to write to a file which will eventually have current updated information on its beginning + not up to date info at its end.

If you want to keep a running output file (and you can't keep the file open), consider this constructor: FileWriter(String, boolean)

If the boolean is true, your updated information will be at the end of the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top