Printable Version Printable Version

Creating a Temporary File

Dec 22nd, 2009 | By admin | Category: Java

We use temporary file in our day to day applications. This post would talk on how to create a temporary file using Java Programming language. Java gives us a direct API to create a temporary file with some options.

A temporary file is a normal file that is created to hold the data temporarily. Efficient memory management is one of the important aspect of any normal application. Creation of temporary files and effective use of them help an application to free the memory in need for other purpose.

A temporary file can be created depending on certain condition and can be closed and deleted depending on some condition. For an example, lets assume we can upload and install a plug-in(a file) on an application. This file can be uploaded as a temporary file in a system defined temporary directory and can be installed from there. On a successful installation completion the temp file can be deleted permanently from the temporary directory.

Lets have a look at the following example ,

Listing 1 : Creating the Temporary file

package com.etechGuide.java.features;

import java.io.File;
import java.io.IOException;

public class TempCreation {
	public static void main(String[] args) {
		try {
			File tempFile = File.createTempFile("mailTogoa", ".ddd");
			System.out.print("Created temporary file with name ");
			System.out.println(tempFile.getAbsolutePath());
			tempFile.deleteOnExit();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

The Line number 9 creates a temporary file. Java IO provides an API like ,
createTempFile(String prefix, String suffix) ;
-Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
In the above programming example , we have used the API to create a temporary file whose prefix is mailTogoa and suffix is .ddd. This file will be created under the system defined temporary directory.
Java IO provides another API to create a temporary file . With the help of this API , you can create a temporary file in user defined location. The method look like this ,
createTempFile(String prefix, String suffix, File directory)
-Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.
Both the above methods are public static method in the java.io.File class.

Line number 12 we call the deleteOnExit() method so that the temporary file can be deleted on JVM exit.

Last thing to be mentioned is , how to get the temporary file location pragmatically. Have a look at the following code list

Listing 2 : Detect the Temporary Directory

package com.etechGuide.java.features;

public class DetectTempDirectory {

	private static final String SYSTEM_PROPERTY_TEMP_FOLDER = "java.io.tmpdir";

	public static void main(String[] args) {
		String sTempFolder = System.getProperty(SYSTEM_PROPERTY_TEMP_FOLDER);
		System.out.println(sTempFolder);
	}
}

java.io.tmpdir property has the operating system specific temporary directory location. In the above example , we have read the property and printed on console.

  • Share/Save/Bookmark
Tags: ,

Leave Comment