Java 临时文件何时删除?

假设我用这个方法在 Java 中创建了一个临时文件

File tmp = File.createTempFile(prefix, suffix);

如果我不明确地调用 delete()方法,文件将在什么时候被删除?

作为一种直觉,它可能是当 JVM 终止时,或者 早些时候(由垃圾收集器) ,或者 回见(由某个操作系统清理进程)。

100332 次浏览

If I do not explicity call the delete() method, when will the file be deleted?

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().

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,

File temp = File.createTempFile("abc", ".tmp");
temp.deleteOnExit();

The above example will create a temporary file named “abc.tmp” and delete it when the program is terminated or exited.

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

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,

File temp = File.createTempFile("abc", ".tmp");
temp.deleteOnExit();

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().

The file won't be deleted automatically, from the JavaDoc:

This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method.

So you have to explicitly call deleteOnExit():

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

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 the File 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:

import java.io.File;
import java.io.RandomAccessFile;
import java.io.IOException;


public class TempFileTest
{
public static void main(String[] args)
{
try {
// create a temp file
File temp = File.createTempFile("tempfiletest", ".tmp");
String path = temp.getAbsolutePath();
System.err.println("Temp file created: " + path);


// open a handle to it
RandomAccessFile fh = new RandomAccessFile (temp, "rw");
System.err.println("Temp file opened for random access.");


// try to delete the file immediately
boolean deleted = false;
try {
deleted = temp.delete();
} catch (SecurityException e) {
// ignore
}


// else delete the file when the program ends
if (deleted) {
System.err.println("Temp file deleted.");
} else {
temp.deleteOnExit();
System.err.println("Temp file scheduled for deletion.");
}


try {
// test writing data to the file
String str = "A quick brown fox jumps over the lazy dog.";
fh.writeUTF(str);
System.err.println("Wrote: " + str);


// test reading the data back from the file
fh.seek(0);
String out = fh.readUTF();
System.err.println("Read: " + out);


} finally {
// close the file
fh.close();
System.err.println("Temp file closed.");
}


} catch (IOException e) {
e.printStackTrace();
}
}
}

On a Unixish system, running this should produce something like the following output:

Temp file created: /tmp/tempfiletest587200103465311579.tmp
Temp file opened for random access.
Temp file deleted.
Wrote: A quick brown fox jumps over the lazy dog.
Read: A quick brown fox jumps over the lazy dog.
Temp file closed.

whereas on Windows, the output looks slightly different:

Temp file created: C:\DOCUME~1\User\LOCALS~1\Temp\tempfiletest5547070005699628548.tmp
Temp file opened for random access.
Temp file scheduled for deletion.
Wrote: A quick brown fox jumps over the lazy dog.
Read: A quick brown fox jumps over the lazy dog.
Temp file closed.

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 same finally block too. I'm not sure what, if anything, using .deleteOnExit() would actually gain you over that.