When are Java temporary files deleted?

2019-01-22 01:01发布

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

6条回答
叼着烟拽天下
2楼-- · 2019-01-22 01:16

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.

查看更多
仙女界的扛把子
3楼-- · 2019-01-22 01:16

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.

查看更多
家丑人穷心不美
4楼-- · 2019-01-22 01:17

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

查看更多
神经病院院长
5楼-- · 2019-01-22 01:24

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.

查看更多
Viruses.
6楼-- · 2019-01-22 01:28

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.

查看更多
淡お忘
7楼-- · 2019-01-22 01:31

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

查看更多
登录 后发表回答