Java java.nio.file.Files createTempDirectory() Method
java.nio.file.Files.createTempDirectory() is a utility method provided in the Java NIO (New I/O) package for creating temporary directories at a specified location or in the default temporary file directory. Temporary directories are typically used to store temporary files needed during program execution; these files are usually deleted after the program ends.
Method overloading
Files The class provides two overloaded createTempDirectory() method in the component:
public static Path createTempDirectory(String prefix, FileAttribute>... attrs)
public static Path createTempDirectory(Path dir, String prefix, FileAttribute>... attrs)
Parameter Description
dir(optional): A Path object specifying where the temporary directory is created. If null or using the first overloaded method, the system's default temporary file directory is used.prefix: The prefix string for the temporary directory name. The generated directory name will start with this prefix.attrs(optional): An optional array of FileAttribute used to set the directory's attributes (such as permissions).
Return Value
Both methods return a Path object pointing to the newly created temporary directory.
Usage examples
Example 1: Create a temporary directory in the default temporary directory
Example
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
public
class
TempDirectoryExample1
{
public
static
void
main
(
String
[
]
args
)
{
try
{
// Create a temporary directory in the default temporary directory with prefix "myApp_"
Path tempDir
=
Files.
createTempDirectory
(
"myApp_"
)
;
System
.
out
.
println
(
"Temporary directory created successfully: "
tempDir
)
;
// Can be manually deleted after use
// Files.delete(tempDir);
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Example 2: Create a temporary directory in a specified directory
Example
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
public
class
TempDirectoryExample2
{
public
static
void
main
(
String
[
]
args
)
{
try
{
// Specify the location to create the directory
Path parentDir
=
Paths.
get
(
"C:/temp"
)
;
// Create a temporary directory in the specified directory with prefix "data_"
Path tempDir
=
Files.
createTempDirectory
(
parentDir,
"data_"
)
;
System
.
out
.
println
(
"Temporary directory created successfully: "
tempDir
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Example 3: Create a temporary directory with specific attributes
Example
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.attribute.PosixFilePermission
;
import
java.nio.file.attribute.PosixFilePermissions
;
import
java.util.Set
;
public
class
TempDirectoryExample3
{
public
static
void
main
(
String
[
]
args
)
{
try
{
// Define directory permissions (only for POSIX-compliant systems)
Set
<
PosixFilePermission
perms
=PosixFilePermissions.
fromString
(
"rwxr-x---"
)
;
FileAttribute
<
Set
<
PosixFilePermission
attr
=PosixFilePermissions.
asFileAttribute
(
perms
)
;
// Create a temporary directory with specific permissions
Path tempDir
=
Files.
createTempDirectory
(
"secure_"
, attr
)
;
System
.
out
.
println
(
"Secure temporary directory created successfully: "
tempDir
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Notes
-
Cleanup of temporary directories:
createTempDirectory()Temporary directories created by the method are not deleted automatically; the program must delete them manually when no longer needed, or usedeleteOnExit()method to mark them for deletion when the JVM exits. -
Use of the prefix: Good prefix naming helps identify the purpose of the temporary directory; it is recommended to use meaningful, application-related names.
-
Concurrency safety: The method ensures that the created temporary directory name is unique, even in a multi-threaded environment.
-
Exception handling: The method may throw IOException, so exceptional cases must be handled properly.
-
Cross-platform considerations: File attributes (such as permissions) may behave differently on different operating systems, especially on Windows and non-POSIX systems.
Practical Application Scenarios
Temporary directories are especially useful in the following scenarios:
- File decompression: extract files to a temporary directory for processing
- File upload: store user-uploaded files for temporary processing
- Data processing: temporary storage used when processing large amounts of data
- Test environment: create isolated temporary workspaces for unit tests
- Caching system: store temporary cache data
By using createTempDirectory() method, developers can conveniently create temporary workspaces without worrying about directory naming conflicts or location selection issues.
Java java.nio.file.Files