Java Program to create a temporary file in different locations

Java program to create a temporary file :

In this tutorial, we will learn how to create a temporary file in Java. We can create a temp file either in the default temporary file location folder or in a specific folder. To handle this, we have two different static methods in the File class. Let’s take a look into them first :

Methods to create temporary file :

public static File createTempFile(String prefix, String suffix)
  • This method creates an empty temp-file in the default temporary file directory. It returns the File object reference using which we can find out the location of the file.

  • The prefix and suffix strings are used to create the final name of the file. The length of the prefix should be atleast 3 character long. suffix can be null . If it is null , “.tmp” is used.

  • It may throw IllegalArgumentException if the prefix contains less than 3 characters, IOException if the file could not be created or SecurityException if any security related problem arises while creating the file.

Now, let’s try to implement it on code :

Java progarm to create temporary file in Default directory :

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

public class Main {

    public static void main(String[] args) {
        try {
            File file1 = File.createTempFile("firstTempFile", null);
            System.out.println("First temp file path " + file1.getAbsolutePath());

            File file2 = File.createTempFile("myTempFile", ".tempSuffix");
            System.out.println("Second temp file path " + file2.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

On my machine it prints something like below :

First temp file path C:\Users\codevscolor\AppData\Local\Temp\firstTempFile11508172464695340971.tmp

Second temp file path C:\Users\codevscolor\AppData\Local\Temp\myTempFile8770312155696418897.tempSuffix

The output will be different on your system. We can see that the first file has a suffix of .tmp since we are passing null in the second parameter. But the second file uses the same suffix that we have passed i.e. .tempSuffix.

One different method is available to create temp-file in a specific directory :

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

It create an empty file in the specified directory. Everything is same as above. The prefix should be at-least 3 characters long. If it is too long, then it will be truncated but first three letters will be same. Same for postfix. If postfix is too long, it will also be truncated. If it is starts with a period, then the period and first 3 characters will be preserved. If suffix is null then .tmp is used. directory variable holds the directory in which the file need to be created. If it is null then the default directory is used. Same as the above method,it may throw IllegalArgumentException,IOException and SecurityException.

Java program to create temp file in a folder :

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

public class Main {

    public static void main(String[] args) {
        try {
            File file = File.createTempFile("firstTempFile", null,new File("C:\myFolder\"));
            System.out.println("First temp file path " + file.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

This will create a temp file inside the myFolder . The name of the file will be different as the above example.

The default temp directory lies inside /tmp or /var/tmp in UNIX system. On windows, it lies inside a Temp folder in C drive.

Similar tutorials :