Suppose I create a temporary file in Java with the method
File tmp = File.createTempFile(prefix, suffix);
If I do not explicity call the delete()
method, when will the file be deleted?
As an intuition, it might be when the JVM terminates, or earlier (by the Garbage Collector), or later (by some Operating System sweeping process).
As the other answers note, temporary files created with
File.createTempFile()
will not be deleted automatically unless you explicitly request it.The generic, portable way to do this is to call
.deleteOnExit()
on theFile
object, which will schedule the file for deletion when the JVM terminates. A slight disadvantage of this method, however, is that it only works if the VM terminates normally; on an abnormal termination (i.e. a VM crash or forced termination of the VM process), the file might remain undeleted.On Unixish systems (such as Linux), it's actually possible to obtain a somewhat more reliable solution by deleting the temporary file immediately after opening it. This works because Unix filesystems allow a file to be deleted (unlinked, to be precise) while it's still held open by one or more processes. Such files can be accessed normally through the open filehandle, and the space they occupy on disk will only be reclaimed by the OS after the last process holding an open handle to the file exits.
So here's the most reliable and portable way I know to ensure that a temporary file will be properly deleted after the program exits:
On a Unixish system, running this should produce something like the following output:
whereas on Windows, the output looks slightly different:
In either case, however, the temp file should not remain on the disk after the program has ended.
Ps. While testing this code on Windows, I observed a rather surprising fact: apparently, merely leaving the temp file unclosed is enough to keep it from being deleted. Of course, this also means that any crash that happens while the temp file is in use will cause it to be left undeleted, which is exactly what we're trying to avoid here.
AFAIK, the only way to avoid this is to ensure that the temp file always gets closed using a
finally
block. Of course, then you could just as well delete the file in the samefinally
block too. I'm not sure what, if anything, using.deleteOnExit()
would actually gain you over that.Temporary file is used to store the less important and temporary data, which should always be deleted when your system is terminated. The best practice is use the File.deleteOnExit() to do it.
For example,
The above example will create a temporary file named “abc.tmp” and delete it when the program is terminated or exited.
It won't, at least not by Java. If you want the file to be deleted when the JVM terminates then you need to call
tmp.deleteOnExit()
.The file won't be deleted automatically, from the JavaDoc:
So you have to explicitly call deleteOnExit():
You can manually delete the file before terminating the process if you want to, however when the process is terminated the file will be deleted as well.
from: How to delete temporary file in Java
For example,
The above example will create a temporary file named “abc.tmp” and delete it when the program is terminated or exited.
If you want to delete the temporary file manually, you can still use the File.delete().