io

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:

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.

Download
You can download the full source code of this example here: TmpDirExample.zip.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button