0

I have a simple xml file, which I update every 3 seconds (by completely replacing contents each time)

I have a problem when computer restarts for any reason (or I long-press the power button to shut it down) - the xml file ends up filled with zero-characters. Same length as proper data, but 0's instead.

I tried saving to tmp file first, validating the data and replacing the original xml file if data seems valid. Did not help. Looks like all validation works fine (SAXBuilder doesnt throw exceptions, I can locate proper child notes etc.), but the file is still corrupted in the end.

I use XMLOutputter and FileWriter to save the data to the temp file. Then replace original with couple renameTo()'s

All works well if I just exit the application or kill the process from task manager. Just the restart/shutdown breaks things.

Any hints on why this is happening will be greatly appreciated.

Stas L
  • 5
  • 4
  • It is happening because you are, err, force-restarting the PC. During the overwrite. Solution: don't. You should certainly save to a temp file, but the validation step is pointless: if you're still executing, just rename the temp file when done. If you're not, it won't happen. – user207421 Mar 17 '17 at 21:52
  • Thing is, the data is still all zeros even if I don't overwrite. I just dont understand at what point the valid xml data turns into null-chars. And why the file is not just incomplete XML due to interrupted write. It has correct length always, but not the contents... (also force restart thing is just to test. Customer gets their config file corrupted due to power outages or something like that) – Stas L Mar 17 '17 at 22:13
  • This is a likely scenario for what's happening: 1) You truncate the file, destroying old contents 2) You write the new file contents, which are held in the page cache, 3) but setting a new file length does make it to disk 4) computer is shut off, data in page cache goes poof! This is probably apropos: https://msdn.microsoft.com/en-us/library/windows/desktop/aa364451(v=vs.85).aspx and especially the first answer here: http://stackoverflow.com/questions/4072878/i-o-concept-flush-vs-sync – Andrew Henle Mar 18 '17 at 01:02

0 Answers0