Java java.nio.file.Files createDirectory() Method
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
dir: The path of the directory to create (Path object)attrs(optional): An optional list of FileAttribute used to set directory attributes (such as permissions)
Return Value
Returns the Path object of the created directory
exception
IOException: If an I/O error occursFileAlreadyExistsException: If the directory already existsSecurityException: If there are not enough permissions to create the directory
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
- Check whether a directory exists: Before attempting to create, you can use
Files.exists(path)Checks - to handle all possible exceptions: Especially
FileAlreadyExistsExceptionandNoSuchFileException - Consider using createDirectories(): If you need to create multi-level directories
- 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 java.nio.file.Files