Java Nio File Createdirectory
[ 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 the `java.nio.file.Files` utility class and provides a simple and secure 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 be created (Path object)
* `attrs` (optional): An optional list of FileAttribute for setting directory attributes (such as permissions)
### Return Value
Returns the Path object of the created directory
### Exceptions
* `IOException`: If an I/O error occurs
* `FileAlreadyExistsException`: If the directory already exists
* `SecurityException`: If there is insufficient permission 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. Creating a Single Directory
`createDirectory()` can only create a single directory. If the parent directory in the path does not exist, it will throw `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 will throw `FileAlreadyExistsException`.
* * *
## Difference from createDirectories()
The `Files` class also provides the `createDirectories()` method. The main differences between the two are:
| Feature | createDirectory() | createDirectories() |
| --- | --- | --- |
| Create single directory | Yes | Yes |
| Create multi-level directories | No | Yes |
| When parent directory does not exist | Throws exception | Automatically creates parent directories |
| When directory already exists | Throws exception | Does not throw exception |
* * *
## Advanced Usage
### 1. Setting 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 perms = PosixFilePermissions.fromString("rwxr-x---");
FileAttribute<Set> attr = PosixFilePermissions.asFileAttribute(perms);
Files.createDirectory(path, attr);
### 2. Handling 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 if directory exists**: Before attempting to create, you can use `Files.exists(path)` to check
2. **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 in Java NIO for creating single directories. It provides richer functionality and better exception handling than the traditional `File.mkdir()`. Understanding this method and its difference from `createDirectories()` can help developers handle file system operations more effectively.
[ Java java.nio.file.Files](#)
YouTip