将Java写入已删除的文件

时间:2021-11-09 00:04:31

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.

我正在使用FileWriter对一个文件进行写操作,并注意到即使我删除了该文件(进程外),FileWriter也不会抛出任何异常。

Is this normal?

这是正常的吗?

3 个解决方案

#1


15  

This depends on your operating system:

这取决于您的操作系统:

  • On Windows, you'd typically not be able to delete an open file.
  • 在Windows上,通常不能删除打开的文件。
  • 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.
  • 在Unix上,删除打开的文件并继续写入(或从其中读取)是完全可以接受的。一旦删除该文件,它将不再具有目录条目。但是,在文件关闭之前,它的内容将继续存在于磁盘上。一旦所有指向该文件的打开句柄被关闭,操作系统将自动回收磁盘空间。

#2


1  

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.

是的,这是正常的。使用大多数执行I/O的常规方法,如果文件不存在,它会为您创建它。当然,这是在你开始写之前删除的。

#3


0  

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}

顺便说一句:如果您查看/proc/{pid}/fd/{fid},您可以读取/写入打开文件的内容(删除与否)

#1


15  

This depends on your operating system:

这取决于您的操作系统:

  • On Windows, you'd typically not be able to delete an open file.
  • 在Windows上,通常不能删除打开的文件。
  • 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.
  • 在Unix上,删除打开的文件并继续写入(或从其中读取)是完全可以接受的。一旦删除该文件,它将不再具有目录条目。但是,在文件关闭之前,它的内容将继续存在于磁盘上。一旦所有指向该文件的打开句柄被关闭,操作系统将自动回收磁盘空间。

#2


1  

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.

是的,这是正常的。使用大多数执行I/O的常规方法,如果文件不存在,它会为您创建它。当然,这是在你开始写之前删除的。

#3


0  

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}

顺便说一句:如果您查看/proc/{pid}/fd/{fid},您可以读取/写入打开文件的内容(删除与否)