Create temporary file in specified directory


/*
  Create temporary file in specified directory
  This Java example shows how to create a new temporary file at specified path using
  createTempFile method of Java File class.
*/

import java.io.*;

public class CreateTempFileDirectory {

 
public static void main(String[] args) {
   
   
/*
     * To create temporary file at specified location use,
     * static File createTempFile(String namePrefix, String nameSuffix, File dir) method
     * of Java File class,
     *
     * where namePrefix is a prefix string used to generate
     * a file's name and must be atleast 3 characters long and nameSuffix is a
     * suffix string used to generate suffix of the temporary file name, may be null,
     * and in that case default ".tmp" will be used as a suffix. dir is the
     * directory under which the temporary file will be created.
     */
    
   
File file = null;
    File dir =
new File("C://FileIO");
   
   
try
   
{
     
file = File.createTempFile("JavaTemp", ".javatemp", dir);
   
}
   
catch(IOException ioe)
    {
   
System.out.println("Exception creating temporary file : " + ioe);
   
}

   
/*
     * Please note that if the directory does not exists, IOException will be
     * thrown and temporary file will not be created.
     */
   
System.out.println("Temporary file created at : " + file.getPath());
 
}
}

/*
Typical output would be
Temporary file created at : C:\FileIO\JavaTemp40534.javatemp
*/