Close a file before we attempt to move it.

I am completely dumbfounded that we've never had trouble with this: we write into a file and then move it, but we don't close it before moving so sometimes nothing is in it yet (presumably if the file is large enough it is actually flushed, but if it's small it isn't). Fix this by closing the file before moving.