How to create a temporary file in Java
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.
- Windows –
%USER%\AppData\Local\Temp
- 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
.
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
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.
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
Temp file : /home/mkyong/test/10425050231787094567.log
If the specified folder does not exist, Java throws NoSuchFileException
.
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.
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.
$ 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
$ 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
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.
package com.mkyong.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/mkyong/core-java
$ cd java-io
if I was surprised by first statement
I was dead by second statement
These files are will be automatically deleted? https://stackoverflow.com/questions/16691437/when-are-java-temporary-files-deleted
Cool! Don’t know about this feature of File class.