File.deleteOnExit - 来自评论的Unix技巧

Lin*_*ton 8 android

以下是此方法的源注释:

请注意,在Android上,应用程序生命周期不包括VM终止,因此调用此方法将无法确保删除文件.相反,你应该使用最合适的:

 * Use a {@code finally} clause to manually invoke {@link #delete}.
 * Maintain your own set of files to delete, and process it at an appropriate point
    in your application's lifecycle.
 * Use the Unix trick of deleting the file as soon as all readers and writers have
   opened it. No new readers/writers will be able to access the file, but all existing
   ones will still have access until the last one closes the file.
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我解释它中提到的"Unix技巧"是什么以及如何使用它?

Cas*_*law 3

这个答案有一个很好的解释:/sf/answers/365397231/。基本上,这意味着在 Unix 系统上“删除”文件并不会立即将其从磁盘上删除;而是会立即将其从磁盘上删除。相反,它只是从该文件所在的目录中删除对该文件的引用。在使用该文件的所有进程终止之前,该文件实际上不会被删除。因此,您可以打开一个临时文件并立即删除它,然后每当程序终止时它就会自动删除。

  • 我明白那个。只是不知道如何使用此功能,因为我从未使用过 Unix 系统。 (2认同)