How to create a temporary file in Java

author image

In Java, we can use the following two Files.createTempFile() methods to create a temporary file in the default temporary-file directory.

                              Path createTempFile(Path dir,                       String prefix,                       String suffix,                       FileAttribute<?>... attrs) throws IOException    Path createTempFile(String prefix,                       String suffix,                       FileAttribute<?>... attrs) throws IOException                          

The default temporary file folder is vary on operating system.

  1. Windows – %USER%\AppData\Local\Temp
  2. Linux – /tmp

1. Create Temporary Files

1.1 This example uses Files.createTempFile(prefix, suffix) to create a temporary file. If the suffix is null, the default is .tmp.

CreateTempFile.java

                              package com.mkyong.io.temp;  import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path;  public class CreateTempFile {      public static void main(String[] args) {          String tmpdir = System.getProperty("java.io.tmpdir");         System.out.println("Temp file path: " + tmpdir);          try {              // Create an temporary file             Path temp = Files.createTempFile("hello", ".file");             System.out.println("Temp file : " + temp);          } catch (IOException e) {             e.printStackTrace();         }      }  }                          

Output

Terminal

                              Temp file path: /tmp   Temp file : /tmp/hello5160971280171236478.file                          

1.2 prefix = null, suffix = ".log"

                              Path temp = Files.createTempFile(null, ".log");   System.out.println("Temp file : " + temp);   // Temp file : /tmp/4024018108441457842.log                          

1.3 prefix = "hello", suffix = null

                              Path temp = Files.createTempFile("hello", null);   System.out.println("Temp file : " + temp);   // Temp file : /tmp/11485851858110293663.tmp                          

1.4 prefix = "hello", suffix = ""

                              Path temp = Files.createTempFile("hello", "");   System.out.println("Temp file : " + temp);   // Temp file : /tmp/hello7797826277559942279                          

1.5 prefix = null, suffix = null

                              Path temp = Files.createTempFile(null, null);   System.out.println("Temp file : " + temp);   // Temp file : /tmp/14576820704460531496.tmp                          

2. Create Temporary Files in a specified directory

2.1 In Java, we can pass a java.nio.file.Path to Files.createTempFile to create a temporary file in a specified directory.

CreateTempFile2.java

                              package com.mkyong.io.temp;  import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths;  public class CreateTempFile2 {      public static void main(String[] args) {          try {              Path path = Paths.get("/home/mkyong/test/");              // Create an temporary file in a specified directory.             Path temp = Files.createTempFile(path, null, ".log");              System.out.println("Temp file : " + temp);          } catch (IOException e) {             e.printStackTrace();         }      }  }                          

Output

Terminal

                              Temp file : /home/mkyong/test/10425050231787094567.log                          

If the specified folder does not exist, Java throws NoSuchFileException.

Terminal

                              java.nio.file.NoSuchFileException: /home/mkyong/test/10614857611703774829.log                          

3. Create Temporary Files + File Permission

3.1 This example tries to assign 777 file permission during temporary file creation.

CreateTempFile3.java

                              package com.mkyong.io.temp;  import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Set;  public class CreateTempFile3 {      public static void main(String[] args) {          try {              // Create a temporary file in a specified folder + file permission             Path path = Paths.get("/home/mkyong/test/");              // 777             Set<PosixFilePermission> fp =                       PosixFilePermissions.fromString("rwxrwxrwx");              Files.createTempFile(path, null, ".log",                       PosixFilePermissions.asFileAttribute(fp));          } catch (IOException e) {             e.printStackTrace();         }      }  }                          

Output – On Unix, the group writes file permission may not work as expected.

Terminal

                              $ ls -lsah  total 8.0K 4.0K drwxr-xr-x  2 mkyong mkyong 4.0K Jul  20 17:22 . 4.0K drwxr-xr-x 42 mkyong mkyong 4.0K Jul  20 16:06 ..     0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 17:22 14080228452578314588.log                          

To fix it, create the temporary file first, and assign the file permission to the temporary file later.

                              Path path = Paths.get("/home/mkyong/test/");    Path tempFile = Files.createTempFile(path, null, ".log");   Files.setPosixFilePermissions(tempFile, PosixFilePermissions.fromString("rwxrwxrwx"));                          

Output

Terminal

                              $ ls -lsah  total 8.0K 4.0K drwxrwxrwx  2 mkyong mkyong 4.0K Jul  20 17:29 . 4.0K drwxr-xr-x 42 mkyong mkyong 4.0K Jul  20 16:06 ..    0 -rwxrwxrwx  1 mkyong mkyong    0 Jul  20 17:29 11536504893440966062.log