Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

Files.createTempFile() and createTempDirectory()

Reference for Files.createTempFile and createTempDirectory: prefixes, suffixes, the default temp location, deleteOnExit, and safe cleanup patterns.

Files.createTempFile(prefix, suffix) creates a new, empty file with a unique name in the system's temporary directory and returns its Path. Files.createTempDirectory(prefix) does the same for a directory. They are the right tools for scratch data, test fixtures and intermediate results.

The name is made from your prefix, a random number, and your suffix. The file is not deleted automatically: you are responsible for cleaning up, by deleting it yourself or by arranging deletion on exit.

Java
Path tmp = Files.createTempFile("report-", ".csv");
Path dir = Files.createTempDirectory("work-");

Try it

Java Creating and cleaning up

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java A temporary working directory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
static Path createTempDirectory(String prefix, FileAttribute<?>... attrs) throws IOException
static Path createTempDirectory(Path dir, String prefix, FileAttribute<?>... attrs) throws IOException
Name Description
dir Directory to create the entry in. Without it, the system temp directory is used.
prefix Start of the name. May be null.
suffix End of the name for files; null means .tmp. Directories have no suffix.
attrs Optional file attributes, such as POSIX permissions.

Returns: the path of the new file or directory.

Throws: IOException if it cannot be created, IllegalArgumentException if the prefix or suffix cannot form a valid name.

Usage notes

  • Location. The default directory comes from the system property java.io.tmpdir (typically /tmp on Linux, a per-user folder on Windows). Do not hard-code it.
  • Permissions. On POSIX systems a temporary file is created readable and writable by the owner only, and a temporary directory likewise, which makes them safe against other users.
  • No automatic cleanup. Delete files in a finally block or with try-with-resources around your own wrapper. tmp.toFile().deleteOnExit() is a last resort: it keeps the path in memory until the JVM exits, and it cannot delete a non-empty directory.
  • Names are unpredictable. Do not print them in output that must be reproducible; test the prefix and suffix instead.
  • Unique and race-free. The file is created atomically, so two programs cannot get the same name (unlike checking exists and then creating).
  • Directories must be emptied before they can be deleted; walk the tree in reverse order.
  • Files in the temp directory may be removed by the operating system at any time, so do not use them for data you cannot recreate.