File createTempFile() method in Java with Examples - GeeksforGeeks

20 Jun, 2021
The createTempFile() function creates a temporary file in a given directory ( if directory is not mentioned then a default directory is selected ), the function generates the filename by using the prefix and suffix passed as the parameters . If the suffix is null then the function uses “.tmp” as suffix. The function then returns the created fileFunction signature:  
1. public static File createTempFile(String PREFIX, String SUFFIX)OR2. public static File createTempFile(String PREFIX, String SUFFIX, File DIR)  
Syntax:  
1. File.createTempFile(String, String, FILE);

2. File.createTempFile(String, String);
Parameters:The function is a overloaded function so one function takes suffix, prefix and a File object, whereas other function takes only suffix and prefix.The prefix must not be less than three characters but the suffix might be null and if the directory is not specified or a null value is passed then the function uses an default directory.Return Type: The function returns the abstract file name denoting the newly created temporary fileException: This method throws:  
IllegalArgumentException: if the prefix argument contains less than three characters
IOExcetion: if there is any IO error(File could not be created)
SecurityException: if the method does not allow file to be created
Below programs illustrates the use of createTempFile() function:Example 1: If we provide the prefix string and provide null suffix string 
Java
// Java program to demonstrate// createTempFile() method of File Class import java.io.*; public class solution {    public static void main(String args[])    {         try {            // create a temp file            File f                = File.createTempFile("geeks", null);             // check if the file is created            if (f.exists()) {                 // the file is created                // as the function returned true                System.out.println("Temp File created: "                                   + f.getName());            }             else {                 // display the file cannot be created                // as the function returned false                System.out.println("Temp File cannot be created: "                                   + f.getName());            }        }         catch (Exception e) {             // display the error message            System.err.println(e);        }    }}
Output:  
Temp File created: geeks7503529537487244659.tmp
Example 2: If we provide the prefix string and a definite suffix string 
Java
// Java Program to demonstrate// createTempFile() method import java.io.*; public class solution {     public static void main(String args[])    {        try {             // create a temp file            File f = File.createTempFile("geeks", ".SVP");             // check if the file is created            if (f.exists()) {                 // the file is created                // as the function returned true                System.out.println("Temp File created: "                                   + f.getName());            }             else {                 // display the file cannot be created                // as the function returned false                System.out.println("Temp File cannot be created: "                                   + f.getName());            }        }         catch (Exception e) {             // display the error message            System.err.println(e);        }    }}
Output:  
Temp File created: geeks4425780422923344328.SVP
Example 3: If we provide the prefix string, a definite suffix string and a directory 
Java
// Java Program to demonstrate// createTempFile() method import java.io.*; public class solution {     public static void main(String args[])    {        try {            // create a temp file            File f = File.createTempFile("geeks",                                         ".SVP",                                         new File("F:"));             // check if the file is created            if (f.exists()) {                 // the file is created                // as the function returned true                System.out.println("Temp File created: "                                   + f.getAbsolutePath());            }             else {                 // display the file cannot be created                // as the function returned false                System.out.println("Temp File cannot be created: "                                   + f.getAbsolutePath());            }        }         catch (Exception e) {             // display the error message            System.err.println(e);        }    }}
Output:  
Temp File created: F:\geeks7006753451952178741.SVP
Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file. 
sooda367
Java-File Class
Java-Functions
Java-IO package
Java Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Java Programming Examples
How to Iterate HashMap in Java?
Iterate through List in Java
Factory method design pattern in Java
Min Heap in Java
Java Program to Remove Duplicate Elements From the Array
Traverse Through a HashMap in Java
Java program to count the occurrence of each character in a string using Hashmap
Iterate Over the Characters of a String in Java
How to Get Elements By Index from HashSet in Java?