问题
I'm using FileWriter to write to a file and noticed that even after I have deleted the file (out of process) the FileWriter does not throw any exception.
Is this normal?
回答1:
This depends on your operating system:
- On Windows, you'd typically not be able to delete an open file.
- On Unix, deleting an open file and continuing to write into it (or read from it) is perfectly acceptable. Once you delete the file, it will no longer have a directory entry. However, its contents will continue to exist on disk until the file is closed. Once all open handles that refer to the file get closed, the OS will automatically reclaim the disk space.
回答2:
Yes, it's normal. Using most of the conventional ways of doing I/O, if the file doesn't exist, it creates it for you. This of course pending that you deleted it before you began writing to it.
回答3:
A file can appear in more than one place (with hard links) Deleting a file removes one of the hard links (possibly the only) If there is a link somewhere else, the file is still accessible.
Files which are deleted, but open continue to take up disk space and if large enough can mean your space used and the size of all the files you can find on your disk is different.
BTW: You can read/write the contents of an open file (deleted or not) if you look in /proc/{pid}/fd/{fid}
来源:https://stackoverflow.com/questions/9164925/java-writing-to-a-deleted-file