Sub topic 3
The Files Class at a Glance
This chapter assumes basic Java syntax, Path, String, List, streams, try/catch, and checked exceptions. It also assumes Java 11 or later, because methods such as Files.readString were added in Java 11.
Files is a utility class in java.nio.file. Its static methods perform operations on paths. A Path represents a location; Files accesses the location.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FilesOverview {
public static void main(String[] args) throws IOException {
Path file = Path.of("notes.txt");
System.out.println(Files.exists(file));
Files.writeString(file, "Learn Path and Files.\n");
System.out.println(Files.isRegularFile(file));
System.out.println(Files.size(file));
System.out.println(Files.readString(file));
Files.deleteIfExists(file);
}
}The example checks whether a file exists, writes text, checks its type, reads its size and contents, and then deletes it. Each operation can fail because of permissions, a missing parent directory, a locked resource, or another file-system problem. That is why main declares throws IOException.
Common operations
Most methods accept a Path and return either a value or no value. Methods that change the file system usually throw IOException.
| Goal | Useful method | Result or effect |
|---|---|---|
| Check a path | `Files.exists(path)` | Returns a boolean |
| Read all text | `Files.readString(path)` | Returns one `String` |
| Read lines | `Files.readAllLines(path)` | Returns `List<String>` |
| Write text | `Files.writeString(path, text)` | Creates or replaces a file |
| Create a directory | `Files.createDirectory(path)` | Creates one directory |
| Copy a path | `Files.copy(source, target)` | Copies a file or directory |
| Move a path | `Files.move(source, target)` | Moves or renames it |
| Remove a path | `Files.delete(path)` | Deletes it |
These methods do not make Path objects valid by themselves. For example, Path.of("missing/notes.txt") creates a perfectly valid path object even when the missing directory does not exist. The failure occurs when a Files method tries to access that location.
Understand method behavior
Some method names look similar but have important differences:
Files.deletethrows an exception if the target does not exist.Files.deleteIfExistsquietly returnsfalsewhen there is nothing to delete.Files.createDirectorycreates only one directory level.Files.createDirectoriesalso creates missing parent directories.Files.isRegularFilereturnsfalsefor directories and for paths that cannot be read as regular files.Files.existsis a check at one moment. The result can become outdated immediately after it returns.
Avoid checking and then assuming an operation must succeed. Another process can change the path between the check and the operation. Prefer the operation itself and handle its exception.
The role of `Files`
Think of the API as a boundary between path descriptions and file-system actions:
Later chapters examine reading, writing, directories, metadata, and safe updates in detail. For now, remember that Files is the main entry point for modern Java file-system operations.
Exercise
What is the difference between Path.of("data.txt") and Files.readString(Path.of("data.txt"))?
Show answer
Path.of creates a Path value that describes a location. Files.readString accesses that location and loads its contents, so it can fail with an IOException.