Files Utility Class Key Methods
Fast recall
Quick revision
Core answer
Files is the static utility class for creating, opening, copying, moving, deleting, inspecting, listing, and reading or writing files and directories through Path values.Interview-ready guide
Interview answer
java.nio.file.Files is a final utility class of static methods that perform file-system work using Path values. It complements Path: Path describes a location, while Files creates, opens, copies, moves, deletes, lists, reads, writes, and inspects the entry at that location.
The important method families are easier to remember than a flat list. Creation includes createFile, createDirectory, createDirectories, and temporary-file methods. Opening includes newInputStream, newOutputStream, newBufferedReader, and newBufferedWriter. Convenience methods such as readString, writeString, readAllBytes, and readAllLines handle bounded content. copy, move, delete, and deleteIfExists modify entries; size, getLastModifiedTime, readAttributes, and predicates inspect them.
Directory work includes list for one level, walk for a lazy recursive stream, find for filtered traversal, and walkFileTree for visitor-based control. Streams returned by listing or walking hold file-system resources and must be closed.
For example, a program can create a temporary directory, create its parent structure, write UTF-8 text, copy the file with REPLACE_EXISTING, and move the copy to a final name. Each method can throw IOException, and its options state whether replacement or other behavior is allowed.
Predicates such as exists are snapshots and may also return false when existence cannot be determined. Another process can change the path immediately after a check, so code should attempt the intended operation and handle FileAlreadyExistsException, NoSuchFileException, or another specific failure. Files is a rich toolbox, but correct use still requires size limits, resource closure, link policy, permissions, and concurrency-aware error handling.
Files Utility Class Key Methods
Direct Answer
Files is the static utility class for creating, opening, copying, moving, deleting, inspecting, listing, and reading or writing files and directories through Path values.
Deep Dive & Architecture
Some Files calls complete before they return and leave no caller-owned resource, such as readString or copy(Path, Path, ...). Others return a stream or channel whose lifetime continues after the call, such as newInputStream, list, and walk. Recognizing that distinction tells the caller whether try-with-resources is required.
Convenience and scalability are also separate. readAllBytes and readString are concise because they collect the whole result. They are not appropriate for arbitrarily large or attacker-controlled files. A bounded stream, channel, or parser can process content incrementally and enforce limits during the operation.
Options express parts of the file-system contract. REPLACE_EXISTING, COPY_ATTRIBUTES, NOFOLLOW_LINKS, CREATE_NEW, and APPEND change behavior, but no option turns every multi-step workflow into one atomic transaction. Even a successful move has provider-dependent details; ATOMIC_MOVE is a requested guarantee that may be unsupported.
Finally, preflight checks are not locks. Files.isWritable(path) cannot guarantee that a later write will succeed, and checking exists before create introduces a race. The operation itself is authoritative. Handle its specific exception and design recovery around the state that may remain after partial failure.
Code Example
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
class FilesOperations {
public static void main(String[] args) throws Exception {
Path root = Files.createTempDirectory("files-api-");
Path source = root.resolve("in/note.txt");
Path copy = root.resolve("copy.txt");
Path moved = root.resolve("final.txt");
try {
Files.createDirectories(source.getParent());
Files.writeString(source, "hello", StandardCharsets.UTF_8);
Files.copy(source, copy, StandardCopyOption.REPLACE_EXISTING);
Files.move(copy, moved, StandardCopyOption.REPLACE_EXISTING);
if (!"hello".equals(Files.readString(moved, StandardCharsets.UTF_8))) {
throw new AssertionError();
}
System.out.println(Files.size(moved));
} finally {
try (var paths = Files.walk(root)) {
for (Path path : paths.sorted((a, b) -> b.compareTo(a)).toList()) {
Files.deleteIfExists(path);
}
}
}
}
}
Check your understanding
Answer these without looking back at the article.
- Which Files methods return resources that the caller must close?
- Why is Files.exists followed by Files.createFile race-prone?
- When should you avoid readAllBytes or readString?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.