Home > Net >  deleting a file before jvm exit on which deleteOnExit is called
deleting a file before jvm exit on which deleteOnExit is called

Time:11-20

Create a java file object and call its deleteOnExit, then call delete programmatically and the close the JVM or program exits

Will deleteOnExit throw any exception as file was already deleted programatically?

CodePudding user response:

If the files is deleted already, nothing will happen. However, it might get deleted if you re-create it.

Even File#delete doesn't throw an exception if the file does not exist, cannot be deleted. It wouls just return false in that case.

However, File#deleteOnExit saves a reference to the File that is kept 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 (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.

  • Related