Use File.createTempFile().

File tempFile = File.createTempFile("prefix-", "-suffix");
//File tempFile = File.createTempFile("MyAppName-", ".tmp");
tempFile.deleteOnExit();

Will create a file in the temp dir, like:

prefix-6340763779352094442-suffix

Answer from Stefan on Stack Overflow
Top answer
1 of 3
234

Use File.createTempFile().

File tempFile = File.createTempFile("prefix-", "-suffix");
//File tempFile = File.createTempFile("MyAppName-", ".tmp");
tempFile.deleteOnExit();

Will create a file in the temp dir, like:

prefix-6340763779352094442-suffix

2 of 3
80

NIO.2

Since Java 7 there is the new file API "NIO2" which contains new methods for creating temp files and directories.

Modern Use default temp folder Specify folder
Create folder Files.createTempDirectory Files.createTempDirectory
Create file Files.createTempFile Files.createTempFile

e.g.

Path tempDir = Files.createTempDirectory("tempfiles");

or

Path tempFile = Files.createTempFile("file", ".tmp");

For more info:

  • Oracle’s online tutorial, free-of-cost: File I/O (Featuring NIO.2)
  • Oracle technical notes: NIO and NIO.2 Examples
  • What Is the Difference Between NIO and NIO.2? at Baeldung.com.
  • Introduction to the Java NIO2 File API at Baeldung.com.

Legacy

Before Java 7, you might use the legacy methods createTempFile found on the File class. Notice the name “File” is singular, in contrast to the Files class in modern NIO.2.

Legacy Use default temp folder Specify folder
Create folder
Create file File.createTempFile File.createTempFile

Security note

An important difference between File.createTempFile() (singular File) and Files.createTempFile (plural Files) is also that the latter has more secure permission defaults.

When no file attributes are specified, then the resulting file may have more restrictive access permissions to files created by the File.createTempFile(String,String,File) method.

🌐
Medium
medium.com › @AlexanderObregon › javas-files-createtempfile-method-explained-be6873fc7de0
Medium
December 27, 2024 - By default, the temporary file is created in the directory specified by the java.io.tmpdir system property. This property points to a location appropriate for temporary storage on the operating system.
🌐
GeeksforGeeks
geeksforgeeks.org › java › create-a-temporary-file-in-java
Create a Temporary File in Java - GeeksforGeeks
July 23, 2025 - The createTempFile() function creates a temporary file in a given directory (if the directory is not mentioned then a default directory is selected), the function generates the filename by using the prefix and suffix passed as the parameters.
🌐
Baeldung
baeldung.com › home › java › java io › creating temporary directories in java
Creating Temporary Directories in Java | Baeldung
1 month ago - One of the most popular approaches used to create temporary directories is to delegate the destination to the underlying operating system. The location is given by the java.io.tmpdir property, and every operating system has its own structure and cleanup routines. In plain Java, we create a directory by specifying the prefix we want the directory to take: String tmpdir = Files.createTempDirectory("tmpDirPrefix").toFile().getAbsolutePath(); String tmpDirsLocation = System.getProperty("java.io.tmpdir"); assertThat(tmpdir).startsWith(tmpDirsLocation);
🌐
Contrastsecurity
support.contrastsecurity.com › hc › en-us › articles › 360044088031-Java-Agent-creates-many-files-in-temp-folder
Java Agent creates many files in temp folder – Contrast Security Support Portal
August 16, 2023 - Note: This only affects Java agents prior to 3.7.9.17038 as more recent agents no longer write out to the tmp folder. The temp folder (generally /tmp or /var/tmp on Linux systems or C:\temp on Windows, but location can differ depending on the application hosting mechanism or server) on my system keeps filling up with files named contrastNNNNNNNNNNN.jar (where N is some digit).
🌐
Apache POI
poi.apache.org › apidocs › dev › org › apache › poi › util › TempFile.html
TempFile (POI API Documentation)
public final class TempFile extends java.lang.Object · Interface for creating temporary files. Collects them all into one directory by default. clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait · public static final java.lang.String JAVA_IO_TMPDIR ·
Find elsewhere
🌐
XWiki
forum.xwiki.org › development
Forbid the use of APIs to create a temporary file in the standard Java temp folder - Development - XWiki Forum
June 27, 2024 - Writing anywhere else than the XWiki temporary or permanent folder is a very bad practice. I just discovered Loading... where I did the mistake (pretty sure I knew it was bad back then, but I still did the mistake), I would like to propose a new Spoon rule to forbid the use of API which makes a bit too easy to make this kind of mistake: File#createTempFile(String, String) Files#createTempDirectory(String, FileAttribute ...)
🌐
Mkyong
mkyong.com › home › java › how to create a temporary file in java
How to create a temporary file in Java - Mkyong.com
July 21, 2020 - Path temp = Files.createTempFile(null, ... In Java, we can pass a java.nio.file.Path to Files.createTempFile to create a temporary file in a specified directory....
🌐
Marc Nuri
blog.marcnuri.com › home › how to get the temporary directory path in java
How to get the temporary directory path in Java - Marc Nuri
January 25, 2026 - Get the temporary directory path in Java using the java.io.tmpdir system property. Includes default paths for Windows, Linux, and macOS.
🌐
Moderne
docs.moderne.io › recipes › catalog › java › security › use secure temporary file creation
Use secure temporary file creation | Moderne Docs
2 weeks ago - import java.io.IOException; class Test { static void method() throws IOException { File tempDir = File.createTempFile("hello", "world"); } } import java.io.File; import java.io.IOException; import java.nio.file.Files; class Test { static void method() throws IOException { File tempDir = Files.createTempFile("hello", "world").toFile(); } } @@ -3,0 +3,1 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; @@ -6,1 +7,1 @@ class Test { static void method() throws IOException { - File tempDir = File.createTempFile("hello", "world"); + File tempDir = Files.createTempFile("hello", "world").toFile(); } This recipe has no required configuration options.
🌐
LabEx
labex.io › tutorials › java-how-to-use-temporary-file-deletion-in-java-419558
How to use temporary file deletion in Java | LabEx
Understanding these techniques is essential for efficient file handling and resource management in Java applications. Temporary files are special files created by applications to store intermediate data during program execution.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › File.html
File (Java Platform SE 8 )
2 days ago - The default temporary-file directory is specified by the system property java.io.tmpdir. On UNIX systems the default value of this property is typically "/tmp" or "/var/tmp"; on Microsoft Windows systems it is typically "C:\\WINNT\\TEMP".
🌐
How to do in Java
howtodoinjava.com › home › i/o › java create temp file or directory
Java Create Temp File or Directory
August 22, 2023 - It creates a new empty temporary file in the specified directory using the given prefix and suffix strings to generate its name. The only difference between both approaches is that it uses the legacy Java classes.
🌐
Codemia
codemia.io › knowledge-hub › path › what_is_a_safe_way_to_create_a_temp_file_in_java
What is a safe way to create a Temp file in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Files.html
Files (Java Platform SE 8 )
April 21, 2026 - As with the File.createTempFile methods, this method is only part of a temporary-file facility. Where used as a work files, the resulting file may be opened using the DELETE_ON_CLOSE option so that the file is deleted when the appropriate close method is invoked.
🌐
Oracle
java.com › en › download › help › plugin_cache.html
How do I clear the Java cache?
In the Java Control Panel, under the General tab, click Settings under the Temporary Internet Files section.
🌐
Tutorialspoint
tutorialspoint.com › java › io › file_createtempfile_directory.htm
Java - File createTempFile(String prefix, String suffix, File directory) method
The Java File createTempFile(String prefix, String suffix, File directory) method creates a new empty file in the specified directory. deleteOnExit() method is called to delete the file created by this method.
🌐
Fredhutch
sciwiki.fredhutch.org › scicompannounce › 2024-01-22-java-temporary-directory
Java Temporary Directory -
January 15, 2026 - Instead it set the default path for temporary files to /tmp. There is much less space available in this path and the contents of that directory are not managed- over time this leads to disk-full conditions which can cause job failures or cause the node to be taken out of service. To address this we are updating the default behavior for Java within a Gizmo job- we are adding the environment variable JAVA_TOOL_OPTIONS and setting it to the job local directory.
🌐
GATK
gatk.broadinstitute.org › hc › en-us › articles › 18965297287067-How-to-setup-and-use-temporary-folder-for-GATK-local-execution
How to setup and use temporary folder for GATK local execution – GATK
GATK tools like many other bioinformatics software rely on temporary files created during execution. However unlike other tools that may use the immediate output folder for temporary files such as samtools or bcftools, GATK and related tools rely on the Java VM's temporary folder setting directly (There are some exceptions but hang on).
🌐
GitHub
github.com › testcontainers › testcontainers-java › issues › 423
Temp files should be created in a temp directory · Issue #423 · testcontainers/testcontainers-java
August 1, 2017 - Currently temp files are created in the current directory. This leads to a mess in case the JVM crashes. Instead the files should be created in a subdirectory of the temp directory.
Author   testcontainers