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.favtuts.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

Temp file path: /tmp
Temp file : /tmp/hello16199335803757730781.file

1.2 prefix = null, suffix = “.log”

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

1.3 prefix = “hello”, suffix = null

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

1.4 prefix = “hello”, suffix = “”

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

1.5 prefix = null, suffix = null

  Path temp = Files.createTempFile(null, null);
  System.out.println("Temp file : " + temp);
  // Temp file : /tmp/6253298279510303633.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.favtuts.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/tvt/workspace/favtuts/temp/");

            // 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

Temp file : /home/tvt/workspace/favtuts/temp/4632902976799078765.log

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

java.nio.file.NoSuchFileException: /home/tvt/workspace/favtuts/temp/8813322845011292158.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.favtuts.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/tvt/workspace/favtuts/temp/");

            // 777
            Set<PosixFilePermission> fp = PosixFilePermissions.fromString("rwxrwxrwx");
            Path temp = Files.createTempFile(path, null, ".log", PosixFilePermissions.asFileAttribute(fp));

            System.out.println("Temp file : " + temp);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output

Temp file : /home/tvt/workspace/favtuts/temp/12906368521357416981.log

$ pwd
/home/tvt/workspace/favtuts/temp

$ ls -lsah
total 8,0K
4,0K drwxrwxr-x  2 tvt tvt 4,0K Thg 5  20 22:42 .
4,0K drwxrwxr-x 12 tvt tvt 4,0K Thg 5  20 22:24 ..
   0 -rwxrwxr-x  1 tvt tvt    0 Thg 5  20 22:42 12906368521357416981.log

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

$ ls -lsah

total 8.0K
4.0K drwxr-xr-x  2 favtuts favtuts 4.0K Jul  20 17:22 .
4.0K drwxr-xr-x 42 favtuts favtuts 4.0K Jul  20 16:06 ..

   0 -rwxr-xr-x  1 favtuts favtuts    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/tvt/workspace/favtuts/temp/");

  Path tempFile = Files.createTempFile(path, null, ".log");
  Files.setPosixFilePermissions(tempFile, PosixFilePermissions.fromString("rwxrwxrwx"));

Output

$ ls -lsah

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

4. Create Temporary Files + Java Legacy IO

This example uses the legacy Java IO java.io.* to create a temporary file, it still works perfectly, but the new Java NIO should always be the first choice.

TempFileCreate4.java

package com.favtuts.io.temp;

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

public class TempFileCreate4 {

    public static void main(String[] args) {

        try {

            File file = File.createTempFile("abc_", ".binary");
            System.out.println(file);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output

/tmp/abc_13872583112153333504.binary

Download Source Code

$ git clone https://github.com/favtuts/java-core-tutorials-examples

$ cd java-io/temp

References

Leave a Reply

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