Creating a temporary file

Creating a temporary file

How to:

In Java, the java.nio.file package is your friend for temporary files. Check out this snippet:

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

public class TempFileExample {
    public static void main(String[] args) {
        try {
            // Create a temporary file
            Path tempFile = Files.createTempFile(null, ".tmp");
            System.out.println("Temporary file created at: " + tempFile);

            // Write to temporary file
            Files.writeString(tempFile, "This is a temporary file content");

            // Read from temporary file
            String content = Files.readString(tempFile);
            System.out.println("Content of the temporary file: " + content);

            // Delete temporary file (optional here as it gets deleted on JVM exit)
            System.out.println("Temporary file deleted.");
        } catch (Exception e) {

Run it, and you get something like:

Temporary file created at: /tmp/user23423842348234823948.tmp
Content of the temporary file: This is a temporary file content
Temporary file deleted.

Neat, right?

Deep Dive

Temporary files have been part of our toolkit for ages, way back to the dawn of computing. They’re your best bet when you need to handle data that has no business sticking around.

Java’s got your back with the Files class since Java 7, making temp file handling super simple. Before that, you’d have to juggle File objects and hope for the best (but don’t go back to those dark days, embrace the new API).

The cool part about the createTempFile method is you can specify the directory and a filename prefix or suffix, or leave it all to Java’s default whims. Just remember, if you don’t manually delete these files, they’ll stick around until the program exits. And in some cases, especially with long-running applications, you’d want to clean up yourself instead of waiting for the big finale.

Alternatives? Sure, you could go old-school and handle every file operation manually, or hook into an OS-specific method. However, the Java way is safer and more portable across platforms.

See Also