Open In App

File createTempFile() method in Java with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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 file
Function signature: 
 

1. public static File createTempFile(String PREFIX, String SUFFIX)
OR
2. 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 file
Exception: 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.
 



Last Updated : 20 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads