GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java Path - working with file and directory paths in Java Java Pathlast modified August 22, 2026
In this article we learn how to work with file and directory paths in Java.
We create paths, inspect their components, combine paths, normalize them, and
use them with the
The
The examples use Java 25's compact source-file syntax. With an earlier Java
version, place the code inside a class and use a conventional
Creating a Path
The Main.java
import java.nio.file.Path;
void main() {
var file = Path.of("reports", "2026", "summary.txt");
var directory = Path.of("reports", "2026");
System.out.println(file);
System.out.println(directory);
}
The path is built from separate components instead of manually joining strings. Java uses the correct separator for the current operating system when it prints the path.
The Relative and absolute paths
A relative path is interpreted from the program's current working directory.
An absolute path identifies a location from the file system root. The
Main.java
import java.nio.file.Path;
void main() {
var relativePath = Path.of("data", "report.txt");
var absolutePath = relativePath.toAbsolutePath();
System.out.println(relativePath.isAbsolute());
System.out.println(absolutePath.isAbsolute());
System.out.println(absolutePath);
}
The first path depends on the process working directory. The second path contains the absolute location obtained from that working directory.
Path components
A path consists of components that can be inspected individually. The
Main.java
import java.nio.file.Path;
void main() {
var path = Path.of("documents", "notes", "today.txt");
System.out.println(path.getFileName());
System.out.println(path.getParent());
System.out.println(path.getNameCount());
System.out.println(path.getName(0));
}
The
The Combining paths with resolve
The Main.java
import java.nio.file.Path;
void main() {
var reports = Path.of("reports");
var summary = reports.resolve("2026").resolve("summary.txt");
System.out.println(summary);
}
The result is
If the argument passed to Resolving a sibling
The var source = Path.of("reports", "summary.txt");
var backup = source.resolveSibling("summary.bak");
The value of Normalizing a path
The Main.java
import java.nio.file.Path;
void main() {
var path = Path.of("reports", "2026", "..", "archive", ".", "summary.txt");
System.out.println(path);
System.out.println(path.normalize());
}
The original path contains redundant components. Its normalized form is
Relativizing paths
The Main.java
import java.nio.file.Path;
void main() {
var source = Path.of("project", "src");
var target = Path.of("project", "test");
var relativePath = source.relativize(target);
System.out.println(relativePath);
}
The result is Checking a Path
The Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
void main() throws IOException {
var path = Path.of("notes.txt");
System.out.println(Files.exists(path));
System.out.println(Files.isRegularFile(path));
System.out.println(Files.isDirectory(path));
if (Files.exists(path)) {
System.out.println(Files.size(path));
}
}
File-system state can change immediately after a check. For operations that matter, handle the exception from the operation itself instead of relying only on a preliminary existence check. Reading and writing with Path
Many methods in Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
void main() throws IOException {
var path = Path.of("notes.txt");
Files.writeString(path, "A note written with Path and Files.");
var contents = Files.readString(path);
System.out.println(contents);
}
If Comparing paths
The
Use Main.java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
void main() throws IOException {
var first = Path.of("notes.txt");
var second = Path.of(".", "notes.txt");
System.out.println(first.equals(second));
if (Files.exists(first) && Files.exists(second)) {
System.out.println(Files.isSameFile(first, second));
}
}
The two paths may have different textual forms even though they identify the
same location after normalization. Converting Path and File
The older Main.java
import java.io.File;
import java.nio.file.Path;
void main() {
var path = Path.of("reports", "summary.txt");
var file = path.toFile();
var samePath = file.toPath();
System.out.println(file);
System.out.println(samePath);
}
The conversions do not create, read, or modify the referenced file. They only create objects representing the same path in the two APIs. Path and SimpleFileVisitor
The Main.java
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
void main() throws IOException {
var root = Path.of("reports");
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
System.out.println(file.getFileName());
return FileVisitResult.CONTINUE;
}
});
}
The visitor receives each file as a Path best practices
SourceJava Path - language reference Java Files - language reference Java SimpleFileVisitor - language reference AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |