Atomically write data to a file.