safer file replacement

Sorry if this is weird because of the merge...no idea if that's normal. I'm new to this whole 'fork and pull request' thing. If you think it's going to be bad I can re-fork or you can just manually make the changes yourself.

Also it might make sense to expose flush() as a member of File. dirname() seems generic, though I didn't see any existing place to put it.

See: https://www.flamingspork.com/talks/2007/06/eat_my_data.odp
and (less complete but an important note about directories): http://linux.die.net/man/2/fsync