How to create temporary file and automatic cleanup in Java - createTempFile(prefix, suffix, dir) and deleteOnExit()

Sometimes we need to create temporary file in system and need to delete the same automatically. In Java we have a static method (createTempFile(prefix, suffix, dir) ) in File class which creates temporary file and provides facility of automatic clean-up. General syntax for this method is :-
public static File createTempFile(String prefix, String suffix,File directory)
prefix
-  minimum three character string(recommended prefix be a short and meaningful string)
suffix - a extension kind of string(.log,.txt), It may be null then ".tmp" will be used for suffix.Suffix can be without a period operator(.)
directory - an optional argument, if provided temporary file will be created at that location, else temp directory of system will be used. For Unix (\tmp or \var\tmp), for windows(C:\\WINNT\\TEMP- NT4 system or C:\Users\<username>\AppData\Local\Temp\ - for Vista/2008/7/8/2012/10).Refer this for more information about default temporary dir in windows.
Temporary file can be  created in following ways depending on arguments passed :- 
Case 1:- public static File createTempFile(String prefix, String suffix,File directory)- temporary file is created at abstract directory location, with file name <prefix><random#><suffix>

Case 2:-
 public static File createTempFile(String prefix, String suffix) - temporary file is created at system dependent temp directory location, with file name <prefix><random#><suffix>

Case 3:- public static File createTempFile(String prefix, null) - temporary file is created at system dependent temp directory location, with file name <prefix><random#>.tmp  (.tmp is default suffix)

How does automatic clean-up is achieved for temporary file ? :- File class has a method deleteOnExit() which can be used with this temporary file. When JVM shut down gracefully, temporary file will be deleted automatically.

Below sample program creates three temporary files considering all three cases discussed above and do clean up for the same. 

package com.devinline.javaio;

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

public class TemporaryFileCreation {

 public static void main(String[] args) {
  File tempDir = new File("E:\\logs\\tmp\\");
  try {
   // create temporary file using prefix, suffix and directory
   File tempFile = File.createTempFile("webClickLog", ".txt", tempDir);
   System.out.println("Case 1:Temp file created "
     + tempFile.getAbsolutePath());
   // Automatic temp file clean up with JVM halt
   tempFile.deleteOnExit();

   // Create temporary file using prefix, suffix and default system
   // dependent temp dir
   File tempFile1 = File.createTempFile("webLogin", ".log");
   System.out.println("\nCase 2:Temp file created "
     + tempFile1.getAbsolutePath());
   // Automatic temp file clean up with JVM halt
   tempFile.deleteOnExit();

   // Create temporary file with default suffix(.tmp)and default
   // directory
   File tempFile2 = File.createTempFile("webClickLog", null);
   System.out.println("\nCase 3:Temp file created "
     + tempFile2.getAbsolutePath());
   // Automatic temp file clean up with JVM halt
   tempFile.deleteOnExit();

  } catch (IOException e) {
   e.printStackTrace();
  }

 }

}
=========Sample output===========
Case 1:Temp file created E:\logs\tmp\webClickLog69104232198436172.txt

Case 2:Temp file created C:\Users\nranjan\AppData\Local\Temp\webLogin4842906534820318825.log

Case 3:Temp file created C:\Users\nranjan\AppData\Local\Temp\webClickLog8224675519388746908.tmp
================================

1 Comments

Previous Post Next Post