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

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

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

Java File Java java.nio.file.Files


java.nio.file.Files.createDirectories() is a utility method provided in the Java NIO (New I/O) package for creating multi-level directory structures. Unlike createDirectory() method,createDirectories() , it automatically creates all non-existent parent directories in the path.

Method Definition

public

static

Path createDirectories

(

Path dir, FileAttribute

...

attrs

)

throws

IOException

Parameter Description

Path dir

FileAttribute... attrs (optional)

Return Value

Exception handling

IOException

- Insufficient permissions - File system full - A name in the path is an existing file rather than a directory

FileAlreadyExistsException

SecurityException


Usage examples

Basic Usage

Example

import

java.nio.file.*

;

public

class

CreateDirectoriesExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"C:/test/demo/example"

)

;

       

       

try

{

            Files.

createDirectories

(

path

)

;

           

System

.

out

.

println

(

"Directory created successfully: "

path

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

err

.

println

(

"Directory creation failed: "

e.

getMessage

(

)

)

;

       

}

   

}

}

Usage with file attributes

Example

import

java.nio.file.*

;

import

java.nio.file.attribute.*

;

public

class

CreateDirectoriesWithAttributes

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"C:/test/demo/example"

)

;

        FileAttribute

<

Set

<

PosixFilePermission

attrs

=

            PosixFilePermissions.

asFileAttribute

(

                PosixFilePermissions.

fromString

(

"rwxr-x---"

)

)

;

       

       

try

{

            Files.

createDirectories

(

path, attrs

)

;

           

System

.

out

.

println

(

"Directory created with permissions successfully: "

path

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

err

.

println

(

"Directory creation failed: "

e.

getMessage

(

)

)

;

       

}

   

}

}


Notes

  1. Atomic operation:createDirectories() is an atomic operation: it either creates all required directories or creates none.

  2. Existing directory: If the directory already exists, the method returns normally without throwing an exception.

  3. Symbolic links: If the path contains symbolic links, the method resolves those links and creates the actual directories.

  4. Performance considerations: Compared to calling multiple times createDirectory(), createDirectories() is more efficient because it reduces the number of file system checks.


The difference between createDirectory() and

Feature createDirectories() createDirectory()
Create multi-level directories Supported Not supported
Behavior when the parent directory does not exist Automatically creates parent directories Throws NoSuchFileException
Atomicity Is Is
Handling of existing directories Returns normally Throws FileAlreadyExistsException

Best Practices

  1. Error handling: Always handle the possible IOException.

  2. Permission check: Check whether there are sufficient permissions before calling.

  3. Path validation: Ensure the path format is correct and contains no illegal characters.

  4. Resource cleanup: If creation fails, consider cleaning up partially created directories.


Frequently Asked Questions

Q: What happens if some directories in the path already exist?

A: The method skips existing directories and only creates the non-existent parts.

Q: Does this method overwrite existing files?

A: No. If a name in the path is an existing file rather than a directory, the method throws FileAlreadyExistsException.

Q: Does this method behave differently on Windows and Linux?

A: The basic behavior is consistent, but the way file attributes (such as permissions) are set may differ by operating system.


Summary

Files.createDirectories() is a very practical method in Java NIO, especially suitable for scenarios that require creating multi-level directory structures. It simplifies the directory creation process, provides atomic operations, and has good error handling. Mastering this method can greatly improve the efficiency and reliability of file system operations.

Java File Java java.nio.file.Files