Just write in a separate file, delete the old and rename the new. Alternatively, read the file into memory, do the manipulation, then write to the file of the name of your choosing.