在调用 deleteOnExit 的 jvm 退出之前删除文件

deleting a file before jvm exit on which deleteOnExit is called

创建一个 java 文件对象并调用其 deleteOnExit,然后以编程方式调用 delete 并关闭 JVM 或程序退出

deleteOnExit 是否会抛出任何异常,因为文件已经以编程方式删除?

如果文件已经被删除,则不会发生任何事情。但是,如果您重新创建它,它可能会被删除。

即使File#delete文件不存在也不会抛出异常,无法删除。在那种情况下,它只是 return false

但是,File#deleteOnExit 保存了对 File 的引用 even if you delete the file manually. Doing this multiple times will result in more and more references to deleted File objects hanging around in the heap, resulting in a memory leak and potentially in an OutOfMemoryError (see here) since you also cannot abort File#deleteOnExit:

Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.