java.io.tmpdir Example
In this tutorial we will discuss about the java.io.tmpdir
system property. The java.io.tmpdir
system property indicates the temporary directory used by the Java Virtual Machine (JVM) to create and store temporary files.
The default value is typically "/tmp"
, or "/var/tmp"
on Unix-like platforms. On Microsoft Windows systems the java.io.tmpdir
property is typically "C:\\WINNT\\TEMP"
.
During your application’s execution, you can acquire and print the value of the java.io.tmpdir
system property, using the following code:
System.out.println(System.getProperty("java.io.tmpdir"));
Change the default value of java.io.tmpdir
In case you want to alter the java.io.tmpdir
system property, you can make use of the -Djava.io.tmpdir
argument and specify your own temporary directory. For example:
java -Djava.io.tmpdir=/home/stathis/Temp
In this way, you alter the value of the java.io.tmpdir
system property, during the initialization of the Java Virtual Machine (JVM). Otherwise, you can use the following snippet, in order to change the value of the java.io.tmpdir
system property:
System.setProperty("java.io.tmpdir", "/home/stathis/Temp");
Create a temporary file
Java provides two static methods in order to create temporary files via the File
class:
public static File createTempFile(String prefix, String suffix, File directory)
:public static File createTempFile(String prefix, String suffix)
:
This method creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
This method creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name. Invoking this method is equivalent to invoking the method createTempFile(prefix, suffix, null)
.
A sample example that creates a number of temporary files is shown below:
TmpDirExample.java:
import java.io.File; import java.io.IOException; public class TmpDirExample { public static void main(String[] args) { String tmpdir = System.getProperty("java.io.tmpdir"); System.out.println("The default value of the java.io.tmpdir system property is: \"" + tmpdir + "\"\n"); //Specify some temporary files. String prefix = "file"; String suffix = ".txt"; File tempFile = null; File tempFile2 = null; File tempFile3 = null; File directory = new File("/home/stathis"); try { //Create two temporary files. tempFile = File.createTempFile(prefix, suffix); tempFile2 = File.createTempFile(prefix, null); tempFile3 = File.createTempFile(prefix, suffix, directory); } catch (IOException ex) { System.err.println("An IOException was caught: " + ex.getMessage()); ex.printStackTrace(); } //Printing the name of every file. System.out.println("A new file called \"" + tempFile.getName() + "\" was created in the directory: \"" + tmpdir + "\""); System.out.println("A new file called \"" + tempFile2.getName() + "\" was created in the directory: \"" + tmpdir + "\"\n"); System.out.println("A new file called \"" + tempFile3.getName() + "\" was created in the directory: \"" + directory.getName() + "\"\n"); //Printing the parent directories of every file. System.out.println("The parent directory of the file \"" + tempFile.getName() + "\" is: \"" + tempFile.getParent() + "\""); System.out.println("The parent directory of the file \"" + tempFile2.getName() + "\" is: \"" + tempFile2.getParent() + "\""); System.out.println("The parent directory of the file \"" + tempFile3.getName() + "\" is: \"" + tempFile3.getParent() + "\"\n"); //Delete the temporary files. if(tempFile.delete()) System.out.println("Successfully deleted the file with name: \"" + tempFile.getName() + "\""); else System.out.println("Couldn't delete the file with name: \"" + tmpdir + "\""); if(tempFile2.delete()) System.out.println("Successfully deleted the file with name: \"" + tempFile2.getName() + "\""); else System.out.println("Couldn't delete the file with name: \"" + tmpdir + "\""); if(tempFile3.delete()) System.out.println("Successfully deleted the file with name: \"" + tempFile3.getName() + "\""); else System.out.println("Couldn't delete the file with name: \"" + tmpdir + "\""); } }
A sample execution is shown below:
The default value of the java.io.tmpdir system property is: "/tmp" A new file called "file4333425833700266479.txt" was created in the directory: "/tmp" A new file called "file3277439988183461403.tmp" was created in the directory: "/tmp" A new file called "file3640333344817114902.txt" was created in the directory: "stathis" The parent directory of the file "file4333425833700266479.txt" is: "/tmp" The parent directory of the file "file3277439988183461403.tmp" is: "/tmp" The parent directory of the file "file3640333344817114902.txt" is: "/home/stathis" Successfully deleted the file with name: "file4333425833700266479.txt" Successfully deleted the file with name: "file3277439988183461403.tmp" Successfully deleted the file with name: "file3640333344817114902.txt"
Notice that the files created by the createTempFile
method have different names during each execution.
You can download the full source code of this example here: TmpDirExample.zip.