Sub topic 12
Creating and Listing Directories
This chapter assumes you know basic Java classes, Path, Files, List, streams, and checked exceptions. It also assumes Java 11 or later. A directory is a file-system location that can contain files and other directories.
Creating directories
Use Files.createDirectory when you want to create exactly one directory. It fails if the parent directory does not exist or if the directory already exists.
Use Files.createDirectories when the complete path may contain missing parent directories. It creates every missing directory and does nothing if the requested directories already exist.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class DirectoryDemo {
public static void main(String[] args) throws IOException {
Path reports = Path.of("data", "reports", "2026");
Files.createDirectories(reports);
System.out.println("Directory: " + reports.toAbsolutePath());
System.out.println("Exists: " + Files.exists(reports));
System.out.println("Is directory: " + Files.isDirectory(reports));
}
}Path.of only builds a path object. It does not create anything. The call to createDirectories changes the file system.
Listing entries in a directory
Files.list returns a Stream<Path> containing the immediate entries in a directory. The entries can be files or subdirectories. It does not recursively visit nested directories.
Because the stream may hold an open directory resource, close it with try-with-resources.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
public class ListDirectory {
public static void main(String[] args) throws IOException {
Path directory = Path.of("data");
try (Stream<Path> entries = Files.list(directory)) {
entries.sorted()
.forEach(entry -> {
String kind = Files.isDirectory(entry)
? "directory"
: "file";
System.out.println(kind + ": " + entry.getFileName());
});
}
}
}The sorted operation sorts paths before printing them. The order returned by Files.list itself is not guaranteed.
Use Files.isDirectory and Files.isRegularFile when the kind of entry matters. These checks can return false when the entry does not exist or cannot be accessed.
Choosing the right operation
| Need | Operation |
|---|---|
| Create one directory whose parent exists | `Files.createDirectory` |
| Create missing parent directories too | `Files.createDirectories` |
| List immediate children | `Files.list` |
| Visit a directory tree recursively | `Files.walk` |
Files.list throws an IOException if the directory cannot be opened. Common causes include a missing path, insufficient permissions, or a path that identifies a regular file instead of a directory. Handle the exception where you can provide a useful response.
Exercise
Which method should you use to create work/output/archive when work and output may not exist, and how should you close the result of listing that directory?
Show answer
Use Files.createDirectories(Path.of("work", "output", "archive")). Close the Stream<Path> returned by Files.list with try-with-resources.