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

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

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

Java File Java java.nio.file.Files


java.nio.file.Files.createDirectory() is a utility method in the Java NIO (New I/O) package for creating new directories. It belongs to java.nio.file.Files utility class, providing a simple and safe way to create a single directory.

Method Definition

public

static

Path createDirectory

(

Path dir, FileAttribute

...

attrs

)

throws

IOException

Parameter Description

Return Value

Returns the Path object of the created directory

exception


Basic Usage Example

Example

import

java.nio.file.*

;

public

class

CreateDirectoryExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"C:/test/new_directory"

)

;

       

       

try

{

            Files.

createDirectory

(

path

)

;

           

System

.

out

.

println

(

"Directory created successfully: "

path

)

;

       

}

catch

(

IOException

e

)

{

           

System

.

err

.

println

(

"Failed to create directory: "

e.

getMessage

(

)

)

;

       

}

   

}

}


Method Characteristics

1. Create a single directory

createDirectory() can only create a single directory; if the parent directory in the path does not exist, it throws NoSuchFileException.

2. Atomic operation

Directory creation is an atomic operation: it either completely succeeds or completely fails.

3. Directory existence check

If the directory already exists, the method throws FileAlreadyExistsException.


Difference from createDirectories()

Files The class also provides the createDirectories() method; the main differences between the two are:

Feature createDirectory() createDirectories()
Create a single directory Is Is
Create multi-level directories No Is
When the parent directory does not exist Throwing an Exception Automatically creates parent directories
When the directory already exists Throwing an Exception Does not throw an exception

Advanced usage

1. Set directory attributes

Example

import

java.nio.file.attribute.PosixFilePermissions

;

import

java.nio.file.attribute.PosixFilePermission

;

import

java.util.Set

;

Path path

=

Paths.

get

(

"/path/to/new_directory"

)

;

Set

<

PosixFilePermission

perms

=

PosixFilePermissions.

fromString

(

"rwxr-x---"

)

;

FileAttribute

<

Set

<

PosixFilePermission

attr

=

PosixFilePermissions.

asFileAttribute

(

perms

)

;

Files.

createDirectory

(

path, attr

)

;

2. Handle exceptions

Example

try

{

    Files.

createDirectory

(

path

)

;

}

catch

(

FileAlreadyExistsException e

)

{

   

System

.

err

.

println

(

"Directory already exists: "

path

)

;

}

catch

(

NoSuchFileException e

)

{

   

System

.

err

.

println

(

"Parent directory does not exist: "

e.

getFile

(

)

)

;

}

catch

(

IOException

e

)

{

   

System

.

err

.

println

(

"Other I/O error: "

e.

getMessage

(

)

)

;

}


Best Practices

  1. Check whether a directory exists: Before attempting to create, you can use Files.exists(path) Checks
  2. to handle all possible exceptions: Especially FileAlreadyExistsException and NoSuchFileException
  3. Consider using createDirectories(): If you need to create multi-level directories
  4. Set appropriate permissions: Especially in multi-user systems

Summary

Files.createDirectory() is the recommended method for creating a single directory in Java NIO; it provides richer functionality and better exception handling than traditional File.mkdir() and better exception handling. Understanding this method and its relationship with createDirectories() helps developers handle file system operations more effectively.

Java File Java java.nio.file.Files