In Java, Files.createTempDirectory(prefix) creates a brand-new, empty folder with a unique name (the prefix plus a random number) in the system temp location and returns its path.
It is the safest place for experiments, tests, and intermediate files, because nothing else uses that folder. It is not deleted automatically: when you are done, delete its contents and then the folder.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.createTempDirectory(...), after import java.nio.file.Files;. You never create a Files object.
Syntax
Give a prefix, and optionally the parent folder:
Java
Path work = Files.createTempDirectory("demo"); // e.g. demo4471230983
Path job = Files.createTempDirectory(work, "job-"); // inside work
Path any = Files.createTempDirectory(null); // digits only
Call
What it does
createTempDirectory(prefix)
New folder in java.io.tmpdir.
createTempDirectory(dir, prefix)
New folder inside an existing dir.
createTempDirectory(prefix, attr)
Also applies attributes, e.g. POSIX permissions.
Since Java 7. On Linux and macOS the folder gets owner-only permissions (rwx------), so other users cannot look inside. Every example on these pages starts with this call.
Library declarations
Java
public static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException
public static Path createTempDirectory(Path dir, String prefix, FileAttribute<?>... attrs) throws IOException
Parameters
Parameter
Description
dir
(Second overload) Existing folder to create the new folder in.
prefix
Start of the generated name, or null.
attrs
Optional attributes applied while creating.
Return Value
Return Value
Description
Path
The new, empty directory. The name is random, so avoid printing it.
Five entries. The workspace holds src, src/main, App.java, and README.txt, plus the workspace folder itself.
Reverse path order. A path sorts after its own folder, so Comparator.reverseOrder() puts every child before its parent; README.txt sorts before src because R comes before s.
Delete as you go. Each folder is already empty when it is reached, so plain Files.delete never sees DirectoryNotEmptyException.
The root has an empty name.work.relativize(work) is an empty path, printed here as (workspace).
Frequently Asked Questions
In the folder named by the java.io.tmpdir system property, for example under /tmp on Linux or the user's AppData\Local\Temp folder on Windows, unless you pass a parent directory.
No. Java does not remove it. Delete the contents and then the folder yourself, or the temp location slowly fills up.
Walk it with Files.walk, sort the paths in reverse order so children come first, and delete each one, or use Files.walkFileTree with a visitor that deletes in postVisitDirectory.
Only the prefix. The rest is a random number that keeps the name unique. Use createDirectory or createDirectories if you need an exact name.
On Linux and macOS the folder is created with owner-only permissions, rwx------. On Windows it inherits the permissions of its parent folder.