How to Create Temporary File in Java

Sometimes in your Java application you may want to store some data in a temporary file that can safely be deleted when the work is done. Java IO and NIO APIs itself provide methods to create a temporary file in Java.

Methods in java.io.File class to create temporary file

In java.io.File class there are two methods:

  • createTempFile(String prefix, String suffix)– Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
  • createTempFile(String prefix, String suffix, File directory)– Creates a temp file in the specified directory, using the given prefix and suffix strings to generate its name. Specified directory should be an existing directory

Temporary file creation Java example using java.io.File class methods

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

public class TempFileCreation {
  public static void main(String[] args) {
    try {
      // Using default temp directory
      File tempFile = File.createTempFile("TempFile", ".temp");
      System.out.println("Temporary file path (Default)- " + tempFile.getCanonicalPath());
      // Specifying directory
      File testFile = File.createTempFile("TempFile", ".temp", new File("F:\\knpcode"));
      System.out.println("Temporary file path- " + testFile.getCanonicalPath());
      // Work with temp file (IO Operations)
      // Delete on exit
      tempFile.deleteOnExit();
      testFile.deleteOnExit();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
}

Output

Temporary file path (Default)- C:\Users\knpcode\AppData\Local\Temp\TempFile8121084790462887465.temp
Temporary file path- F:\knpcode\TempFile7094477988758089030.temp

Note that deleteOnExit() method is used to delete the file when the virtual machine terminates.

Methods in java.nio.file.Files class to create temporary file

In java.nio.file.Files class there are two methods:

  • createTempFile(String prefix, String suffix, FileAttribute<?>… attrs)– Creates temp file in the default temporary-file directory, using the given prefix and suffix to generate its name.
  • createTempFile(Path dir, String prefix, String suffix, FileAttribute<?>… attrs)– Creates temp file in the specified directory, using the given prefix and suffix strings to generate its name.

Temporary file creation Java example using java.nio.file.Files class methods

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

public class TempFileCreation {
  public static void main(String[] args) {
	  try {
      // Using default directory
      Path tempFile = Files.createTempFile("TempFile", ".temp");
      System.out.println("Temporary file path (Default)- " + tempFile);
      // Specifying directory
      Path testFile = Files.createTempFile(Path.of("F:\\knpcode"), "TempFile", ".temp");
      System.out.println("Temporary file path- " + testFile);
      // Work with temp file (IO Operations)
      // Delete on exit  
      tempFile.toFile().deleteOnExit();
      testFile.toFile().deleteOnExit();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } 
  }
}

Output

Temporary file path (Default)- C:\Users\knpcode\AppData\Local\Temp\TempFile14952623994848508190.temp
Temporary file path- F:\knpcode\TempFile10342615863071845696.temp

Related Posts

That’s all for the topic How to Create Temporary File in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.