Java - File createTempFile() Method



Description

The Java File createTempFile(String prefix, String suffix, File directory) method creates a new empty file in the specified directory. deleteOnExit() method is called to delete the file created by this method.

Declaration

Following is the declaration for java.io.File.createTempFile(String prefix, String suffix, File directory) method −

public static File createTempFile(String prefix, String suffix, File directory)

Parameters

  • prefix − The prefix string defines the files name; must be at least three characters long

  • suffix − The suffix string defines the file's extension; if null the suffix ".tmp" will be used

  • directory − The directory in which the file is to be created. For default temporary-file directory null is to passed

Return Value

An abstract pathname for the newly-created empty file.

Exception

  • IllegalArgumentException − If the prefix argument contains less than three characters

  • IOException − If a file creation failed

  • SecurityException − If SecurityManager.checkWrite(java.lang.String) method does not allow a file to be created

Example 1

The following example shows the usage of Java File createTempFile() method. We've created a File reference. Using createTempFile() method, we're creating the file in temporary directory and printing the absolute path of the file created. We're passing parameters to createTempFile() as tmp, .txt and file path of directory to create the temporary file with prefix as tmp and suffix as .txt.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
            
      try {
         // creates temporary file
         f = File.createTempFile("tmp", ".txt", new File("F:/"));
         
         // prints absolute path
         System.out.println("File path: "+f.getAbsolutePath());         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

File path: F:\tmp11479871496507253874.txt

Example 2

The following example shows the usage of Java File createTempFile() method. We've created a File reference. Using createTempFile() method, we're creating the file in temporary directory and printing the absolute path of the file created. We're passing both parameters to createTempFile() as tmp, null and file path of directory to create the temporary file with prefix as tmp and suffix as .tmp.

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
            
      try {
         // creates temporary file
         f = File.createTempFile("tmp", null, new File("F:/"));
         
         // prints absolute path
         System.out.println("File path: "+f.getAbsolutePath());         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

File path: F:\tmp11732077494906020658.tmp

Example 3

The following example shows the usage of Java File createTempFile() method. We've created a File reference. Using createTempFile() method, we're creating the file in temporary directory and printing the absolute path of the file created. We're passing both parameters to createTempFile() as tm, .txt and file path of directory to create the temporary file with prefix as tm and suffix as .text. It is to check if we're getting exception or not for too short prefix name.

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
            
      try {
         // creates temporary file
         f = File.createTempFile("tm", ".txt", new File("F:/"));
         
         // prints absolute path
         System.out.println("File path: "+f.getAbsolutePath());         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.lang.IllegalArgumentException: Prefix string "tm" too short: length must be at least 3
	at java.base/java.io.File.createTempFile(File.java:2104)
	at FileDemo.main(FileDemo.java:9)
java_file_class.htm
Advertisements