Create Temporary File in Java

Tags: Java File Java IO Java NIO

In this tutorial, we are going to learn how to create a new temporary file in a Java program by using core Java API.

Using Java NIO Files.createTempFile() method to create temporary file

The following Java example program to create a new temporary file in the default temporary directory in your machine. In order to create a new temp file you need to provide file name prefix and file extension (or file suffix), the method creates a new file with a unique name in your temporary directory.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CreateTempFileExample1 {
    public static void main(String[] args) {
        try {
            Path tempFilePath = Files.createTempFile("temp-file-name", ".txt");

            System.out.println("Create new temp file at " + tempFilePath.toAbsolutePath().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Create new temp file at C:\Users\SimpleSolution\AppData\Local\Temp\temp-file-name1036663710936783689.txt

The Files.createTempFile() method also allows you to provide a specific temporary directory as following example.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CreateTempFileExample2 {
    public static void main(String[] args) {
        try {
            Path tempDirectory = Paths.get("C:\\temp");
            Path tempFilePath = Files.createTempFile(tempDirectory, "temp-file-name", ".txt");

            System.out.println("Create new temp file at " + tempFilePath.toAbsolutePath().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Create new temp file at C:\temp\temp-file-name2855756936050279179.txt

The below example to show how to write text to a new temporary file.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class CreateTempFileExample3 {
    public static void main(String[] args) {
        try {
            Path tempFilePath = Files.createTempFile("temp-file-name", ".txt");
            Files.write(tempFilePath, "data".getBytes());

            System.out.println("Write Data to file: " + tempFilePath.toAbsolutePath().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Write Data to file: C:\Users\SimpleSolution\AppData\Local\Temp\temp-file-name15174916502158845443.txt

Using Java IO File.createTempFile() method to create temporary file

Following example using File.createTempFile() method to create temp in in default temporary directory.

import java.io.File;
import java.io.IOException;

public class CreateTempFileExample4 {
    public static void main(String[] args) {
        try {
            File tempFile = File.createTempFile("temp-file", ".txt");

            System.out.println(tempFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
C:\Users\SimpleSolution\AppData\Local\Temp\temp-file6166404873986883374.txt

File.createTempFile() static method also provides an override to allow you choose a specific temporary directory to create the temp file.

import java.io.File;
import java.io.IOException;

public class CreateTempFileExample5 {
    public static void main(String[] args) {
        try {
            File tempDirectory = new File("C:\\temp");
            File tempFile = File.createTempFile("temp-file", ".txt", tempDirectory);

            System.out.println(tempFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
C:\temp\temp-file10789004977724301237.txt

In the below example we use FileWriter to write text to the new temporary file.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateTempFileExample6 {
    public static void main(String[] args) {
        try {
            File tempFile = File.createTempFile("temp-file", ".txt");
            FileWriter fileWriter = new FileWriter(tempFile);
            fileWriter.write("Simple Solution");
            fileWriter.close();

            System.out.println("Write data to file: " + tempFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
The output is:
Write data to file: C:\Users\SimpleSolution\AppData\Local\Temp\temp-file6557335660497399063.txt

Happy Coding 😊

Create New Files in Java

Write Text to a File in Java

Get Absolute File Path of a File in Java

Get File Size in Java

Check Hidden Files in Java