Replacing the content of a file is a very slow operation. Compared to replacing the content of a variable, it's usually a few times slower; when we write something to disk, it takes time before it's actually flushed and time before the content is actually written to disk. It's not an atomic operation, so if our software faces any issues while saving a file, there is a good chance that the file might end up being half-written and our users don't have a way to recover the consistent state of their data.
There is a pattern commonly used to solve this kind of issue, which is based on the fact that writing a file is a slow, expensive, error-prone operation, but renaming a file is an atomic, fast, and cheap operation.