Java - File createTempFile() Method



Description

The Java File createTempFile(String prefix, String suffix) method atomically creates an empty file in the default temporary folder.

Declaration

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

public static File createTempFile(String prefix, String suffix)

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

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 both parameters to createTempFile() as tmp and .txt 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");
         
         // 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: C:\Users\intel\AppData\Local\Temp\tmp3526113184482496754.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 and null 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);
         
         // 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: C:\Users\intel\AppData\Local\Temp\tmp2433347031187124180.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 and .txt 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");
         
         // 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 java.base/java.io.File.createTempFile(File.java:2175)
	at FileDemo.main(FileDemo.java:9)
java_file_class.htm
Advertisements