Java writing to a deleted file

后端 未结 3 1969
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 08:16

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 no

相关标签:
3条回答
  • 2021-01-04 08:25

    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}

    0 讨论(0)
  • 2021-01-04 08:41

    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.

    0 讨论(0)
  • 2021-01-04 08:43

    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.
    0 讨论(0)
提交回复
热议问题