Java File I/O FAQ: How do I create a Java temporary file? Also, after I create a temporary file, when is it deleted?
I just ran into this question on the Mac/Java mailing list, so I thought I'd write a quick "Java temporary file" test to explore this. Here are the results. Note that the same approach also works with Scala.
Creating a Java temporary file
Creating a temporary file in Java is straightforward. There are two static methods named createTempFile
in the Java File class, one that takes two arguments, and another that takes three arguments. I created the test class shown below to demonstrate (a) how to create a Java temporary file, and (b) to see when the Java temporary file was deleted.
Here's the source code for my Java temporary file example:
import java.io.File; import java.io.IOException; /** * JavaTemporaryFiles.java * A test class to demonstrate how to create a Java temporary file. * * @author Alvin Alexander, https://alvinalexander.com * */ public class JavaTemporaryFiles { public static void main(String[] args) throws IOException { String prefix = "foobar"; String suffix = ".tmp"; // this temporary file remains after the jvm exits File tempFile = File.createTempFile(prefix, suffix); System.out.format("Canonical filename: %s\n", tempFile.getCanonicalFile()); } }
Results of the Java temporary file example
I used the easier createTempFile
method, and it requires that you supply two arguments, a filename prefix and a filename suffix, so I used the strings shown above.
When I ran this code on my Mac OS X 10.6 system, using Java 6.x, a temporary file with the following name was created:
/private/var/folders/h5/h59HESVvEmG+3I4Q8lOAxE+++TI/-Tmp-/foobar3211893616186530360.tmp
Also -- and very importantly -- this temporary file was not deleted after my Java program exited. I left it there for a while as I ran other tests, and finally deleted it manually.
A second Java temporary file example
Next, I created a second Java temporary file example, this time calling the Java File class deleteOnExit
method, as shown here:
import java.io.File; import java.io.IOException; /** * JavaTemporaryFiles2.java * A test class to demonstrate how Java temporary files work, including * requesting that the temporary file be deleted when the JVM exits, * using the deleteOnExit() method call. * * @author Alvin Alexander, http://devdaily.com * */ public class JavaTemporaryFiles2 { public static void main(String[] args) throws IOException { String prefix = "foobar"; String suffix = ".tmp"; // by calling deleteOnExit the temp file is deleted when the jvm is // shut down File tempFile2 = File.createTempFile(prefix, suffix); tempFile2.deleteOnExit(); System.out.format("Canonical filename: %s\n", tempFile2.getCanonicalFile()); } }
In this case a different temporary file was created, and very importantly, it was also deleted when the JVM exited. The deleteOnExit
argument does indeed make a huge difference in how your Java temporary file is treated, at least on a Mac OS X system.
Java temporary files and notes from the Javadoc
As a final piece of information, here are some notes from the Java File class Javadoc regarding Java temporary files:
First, the three-argument version of the createTempFile
method implies that you have to call the deleteOnExit
method to get rid of the file:
"To arrange for a file created by this method to be deleted automatically, use the deleteOnExit() method."
Second, the deleteOnExit
Javadoc provides these pieces of information:
"Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates ... Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification."
Java temporary files - summary
I hope this Java temporary file tutorial has been helpful. As you've seen, just use createTempFile
to create a temporary file in Java, and then use deleteOnExit
if you want to make sure that your temporary file is deleted when your Java application exits.