Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java java.nio.file.Files createTempDirectory() Method

Java java.nio.file.Files createTempDirectory() Method

Java File Java java.nio.file.Files


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

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

  1. 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 use deleteOnExit() method to mark them for deletion when the JVM exits.

  2. Use of the prefix: Good prefix naming helps identify the purpose of the temporary directory; it is recommended to use meaningful, application-related names.

  3. Concurrency safety: The method ensures that the created temporary directory name is unique, even in a multi-threaded environment.

  4. Exception handling: The method may throw IOException, so exceptional cases must be handled properly.

  5. 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:

  1. File decompression: extract files to a temporary directory for processing
  2. File upload: store user-uploaded files for temporary processing
  3. Data processing: temporary storage used when processing large amounts of data
  4. Test environment: create isolated temporary workspaces for unit tests
  5. 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 File Java java.nio.file.Files