Make sure the file is deleted when exiting the JVM

Does File.deleteOnExit () provide that the file is deleted even if the JVM is killed early?

+5
source share
2 answers

Uninstallation will only be attempted for the normal completion of the virtual machine, as defined by the Java Language Specification

No. check the file the next time you run it, if possible.

+4
source

As Tim Bender notes, he File.deleteOnExit()does not guarantee that the file will be deleted.

Unixish (, Linux OSX), , ( ). , , , , , , ( ).

Windows, . , , deleteOnExit():

File tempFile = File.createTempFile("tempfile", ".tmp");
RandomAccessFile fh = new RandomAccessFile (tempFile, "rw");

// try to delete the file now, fall back to deletion on exit
if ( !tempFile.delete() ) tempFile.deleteOnExit();

, , -, File.deleteOnExit() Windows. , , , . . , .

+4

All Articles