io

java.io.tmpdir Example

Photo of Sotirios-Efstathios ManeasSotirios-Efstathios ManeasSeptember 17th, 2014Last Updated: November 9th, 2014
0 4,533 3 minutes read

In this tutorial we will discuss about thejava.io.tmpdir system property. Thejava.io.tmpdir system property indicates thetemporary 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 thejava.io.tmpdir property is typically"C:\\WINNT\\TEMP".

During your application’s execution, you can acquire and print the value of thejava.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 thejava.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 thejava.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 thejava.io.tmpdir system property:

System.setProperty("java.io.tmpdir", "/home/stathis/Temp");

Create a temporary file

Java provides twostatic methods in order to create temporary files via theFile class:

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() + "\"");elseSystem.out.println("Couldn't delete the file with name: \"" + tmpdir + "\"");if(tempFile2.delete())System.out.println("Successfully deleted the file with name: \""+ tempFile2.getName() + "\"");elseSystem.out.println("Couldn't delete the file with name: \"" + tmpdir + "\"");if(tempFile3.delete())System.out.println("Successfully deleted the file with name: \""+ tempFile3.getName() + "\"");elseSystem.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 thecreateTempFile method have different names during each execution.

Download
You can download the full source code of this example here:TmpDirExample.zip.
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Sotirios-Efstathios ManeasSotirios-Efstathios ManeasSeptember 17th, 2014Last Updated: November 9th, 2014
0 4,533 3 minutes read
Photo of Sotirios-Efstathios Maneas

Sotirios-Efstathios Maneas

Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.