NIO.2 Path and Files Classes
NIO.2 is the modern file API in Java, in the java.nio.file package, added in Java 7. It has two central types. Path is an interface that represents a location in the file system, such as school/2026/marks.txt; it doesn't open or touch the file. Files is a utility class of static methods that do the actual work: create, read, write, copy, move, delete and inspect files. In this lesson, you will learn how to build and work with paths, and the most important methods of Files.
Creating a Path
| Way | Example |
|---|---|
| From parts | Path.of("school", "2026", "marks.txt") |
| From one string | Path.of("school/2026/marks.txt") |
From a File | file.toPath() |
| Relative to another path | dir.resolve("marks.txt") |
Paths.get(...) is an older equivalent of Path.of(...).
Methods of Path
| Method | Description |
|---|---|
getFileName() | It is used to get the last part, such as marks.txt. |
getParent() | It returns the path without the last part. |
getRoot() | It returns the root (/ or C:\), or null for relative paths. |
getNameCount(), getName(i) | It counts and returns the parts. |
resolve(other) | It joins a child path onto this one. |
resolveSibling(name) | It replaces the last part. |
relativize(other) | It returns the path from this one to another. |
normalize() | It removes . and .. parts. |
toAbsolutePath() | It turns a relative path into a full one. |
startsWith, endsWith | It compares path parts. |
Example: Working with Paths
Let's see an example of path operations. None of these files need to exist.
import java.nio.file.Path;
public class PathDemo {
public static void main(String[] args) {
Path marks = Path.of("school", "2026", "marks.txt");
System.out.println("Path: " + marks);
System.out.println("File name: " + marks.getFileName());
System.out.println("Parent: " + marks.getParent());
System.out.println("Parts: " + marks.getNameCount());
System.out.println("Absolute? " + marks.isAbsolute());
Path report = marks.resolveSibling("report.pdf");
System.out.println("Sibling: " + report);
Path messy = Path.of("school/2026/../2025/./notes.txt");
System.out.println("Normalized: " + messy.normalize());
Path base = Path.of("school");
System.out.println("Relative: " + base.relativize(marks));
System.out.println("Ends with marks.txt? "
+ marks.endsWith("marks.txt"));
}
}Path: school/2026/marks.txt
File name: marks.txt
Parent: school/2026
Parts: 3
Absolute? false
Sibling: school/2026/report.pdf
Normalized: school/2025/notes.txt
Relative: 2026/marks.txt
Ends with marks.txt? trueExplanation: Path.of joins the parts with the correct separator (/ on macOS and Linux, \ on Windows). resolveSibling swaps the file name. normalize removes .. (which cancels 2026) and .. relativize gives the path from school to the file.
Methods of Files
| Group | Methods |
|---|---|
| Check | exists, notExists, isRegularFile, isDirectory, isReadable, isWritable, size |
| Create | createFile, createDirectory, createDirectories, createTempFile |
| Read | readString, readAllLines, readAllBytes, lines, newBufferedReader, newInputStream |
| Write | writeString, write, newBufferedWriter, newOutputStream |
| Manage | copy, move, delete, deleteIfExists |
| Folders | list, walk, find, newDirectoryStream |
| Attributes | getLastModifiedTime, setLastModifiedTime, getOwner, readAttributes |
Example: Inspecting a File
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Inspect {
public static void main(String[] args) throws IOException {
Path file = Path.of("diary.txt");
Files.writeString(file, "Dear diary, today Advitha learned NIO.2.");
System.out.println("Regular file? " + Files.isRegularFile(file));
System.out.println("Directory? " + Files.isDirectory(file));
System.out.println("Readable? " + Files.isReadable(file));
System.out.println("Size: " + Files.size(file) + " bytes");
System.out.println("Exists other? "
+ Files.exists(Path.of("nope.txt")));
}
}Regular file? true
Directory? false
Readable? true
Size: 40 bytes
Exists other? falseExplanation: Files.writeString creates the file with the text. The is... methods check what kind of entry it is and what the program may do with it, and size returns its length in bytes.
Common Mistakes
Expecting Path.of("a.txt") to create a file. Use Files.createFile or a write method.
Forgetting normalize() before comparing paths. a/../b and b are different Path objects until normalized.
Using string concatenation for paths. Use resolve.
Interview Corner
What are the Path and Files classes in Java?
Path (in java.nio.file) is an interface representing a hierarchical file system location, created with Path.of(...); it supports operations on the path itself such as resolve, relativize, normalize and getParent, without accessing the file system. Files is a utility class of static methods that operate on the file system using paths: checking, creating, reading, writing, copying, moving, deleting, listing and walking files and directories, and reading attributes. Together they form the NIO.2 API introduced in Java 7.
Practice
- Easy: Build the path
reports/2026/november.csvand print its file name, parent and number of parts. - Harder: Write a method that changes a file's extension, such as
marks.txttomarks.csv, usingresolveSiblingand string methods.
Key Takeaways
Was this tutorial helpful?