Open In App

Create a Temporary File in Java

Last Updated : 08 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, we can create a temporary file using an existing method known as File.createTempFile() method which creates a new empty file on the specified directory. The createTempFile() function creates a temporary file in a given directory (if the 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

Scenarios: There are two scenarios in which we can create a Temporary File.

  1. createTempFile(prefix,suffix,directory)
  2. createTempFile(prefix,suffix,null)

Method — Scenario 1 

This method creates a new and empty file in the specified directory, using the given prefix and suffix to generate its name.

Syntax:

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

Parameters: It takes 3 parameters- prefix, suffix, and the directory.

  1. Prefix: The prefix string is used in generating the file’s name, must be at least three characters long.
  2. Suffix: The suffix string is used in generating the file’s name.It can be ‘.txt’ or ‘null’.In case of ‘null’ “.tmp” will be used.
  3. Directory: The directory in which the file is to be created, or null if the default temporary-file directory is to be used.

Return Type: An abstract pathname denoting a newly-created empty file

Exceptions:

  • IllegalArgumentExpression: If prefix argument contains less than three characters.
  • IOException: If a file could not be created.
  • SecurityException: If a security manager exists and its SecurityManager and checkWrite() method does not allow a file to be created.

Method — Scenario 2 

This method creates a new and empty file in the default temporary file-directory, using the given prefix and suffix to generate its name.Invoking this method is equivalent to invoking createTempFile(prefix,suffix,null).

Syntax:

public static File createTempFile(String prefix,String suffix)

Parameters: It only takes two parameters- prefix and suffix

  1. Prefix: The prefix string is used in generating the file’s name,must be at least three characters long
  2. Suffix: The suffix string is used in generating the file’s name.It can be ‘.txt’ or ‘null’.In case of ‘null’ “.tmp” will be used

Return Type: An abstract pathname denoting a newly-created empty file

Exceptions:

  1. IllegalArgumentExpression: If prefix argument contains less than three characters.
  2. IOException: If a file could not be created.
  3. SecurityException: If a security manager exists and its SecurityManager and checkWrite() method does not allow a file to be created.

Example

Java




// Java Program to Create a Temporary File in Java
 
// Importing all input output classes
import java.io.File;
import java.io.IOException;
 
//  Class
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
        throws IOException
    {
 
        // Try block to check for exceptions
        try {
 
            // Step 1
            // Creating temporary file with default location
            // by creating an object of File type
            File obj1 = File.createTempFile("temp", ".txt");
 
            // Step 2
            // Obtaining absolute path of the path
            // returned by createTempfile() function
            String path = obj1.getAbsolutePath();
 
            // Step 3
            // Print and display the default path of
            // temporary file
            System.out.println(
                "Path of temporary file with default location:"
                + path);
 
            // Step 4
            // Creating temporary file with specified
            // location again by creating another object of
            // File type which is custom local directory
            File obj2 = File.createTempFile(
                "temp", ".txt",
                new File(
                    "C:/Users/ASPIRE/Desktop/my folder"));
 
            // Step 5
            // Obtaining absolute path of the path
            // returned by createTempfile() function
            path = obj2.getAbsolutePath();
 
            // Step 6
            // Print and display the specified path of
            // temporary file
            System.out.println(
                "Path of temporary file with specified location:"
                + path);
        }
 
        // Catch block to handle exception if occurs
        catch (IOException e) {
 
            // Print the line number where exception occur
            // using printStackTrace() method
            e.printStackTrace();
        }
    }
}


Output:

Below snapshots are also appended at these directories in a local computer which is as follows:

The case where the temporary file is created at the given path

The case where the temporary file is created at the default path



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads