Java java.nio.file.Files createDirectories() Method
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
- The directory path to create
- Type:
java.nio.file.Path - cannot be null
FileAttribute>... attrs (optional)
- Optional parameters for setting directory attributes
- Type: variable arguments
FileAttribute> - Can specify attributes such as file permissions, owner, etc.
Return Value
- Returns the created directory's
Pathobject - If the directory already exists, returns that directory's
Pathobject
Exception handling
IOException
- Thrown when the directory cannot be created
- Common causes:
- Insufficient permissions
- File system is full
- A name in the path is an existing file rather than a directory
FileAlreadyExistsException
- Thrown when a name in the path is an existing file rather than a directory
SecurityException
- Thrown when the security manager denies access
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
-
Atomic operation:
createDirectories()is an atomic operation: it either creates all required directories or creates none. -
Existing directory: If the directory already exists, the method returns normally without throwing an exception.
-
Symbolic links: If the path contains symbolic links, the method resolves those links and creates the actual directories.
-
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
-
Error handling: Always handle the possible
IOException. -
Permission check: Check whether there are sufficient permissions before calling.
-
Path validation: Ensure the path format is correct and contains no illegal characters.
-
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 java.nio.file.Files