In Java, dir.resolve(other)joinsother onto dir with the correct separator: Paths.get("/home/anna").resolve("docs/report.txt") is /home/anna/docs/report.txt. It is the safe replacement for gluing strings together with "/".
Two special cases: if other is absolute, it is returned as it is, and if other is empty, dir itself is returned. Like all Path methods, resolve does not touch the disk and does not clean up . or ...
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.resolve(...), after import java.nio.file.Path;.
Syntax
Pass the child as a String or a Path; the original path does not change:
Java
Path home = Paths.get("/home/anna");
Path report = home.resolve("docs/report.txt"); // /home/anna/docs/report.txt
Path song = home.resolve(Paths.get("music", "a.mp3")); // /home/anna/music/a.mp3
Call
What it does
dir.resolve("name.txt")
A file inside dir.
dir.resolve("a").resolve("b.txt")
Chain calls to go deeper.
dir.resolve(userInput).normalize()
Join, then remove .. before checking it.
Since Java 7; the String overload is a default method from Java 11. Java 22 added varargs overloads, such as resolve("a", "b.txt"); on older versions chain the calls.
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Join Different Children onto a Home Folder
Resolve a folder name, a relative file path, a Path object, an absolute path, and an empty string.
Run with java PathResolveAppTree. Shown for Linux and macOS. Windows prints the same paths with \ between the names.
settings: myapp/config/settings.properties
log: myapp/logs/2026-11.log
cache: myapp/cache/images
app is still: myapp
How It Works
One base, many locations. Every path is derived from app, so moving the application means changing a single string.
Chain for depth.app.resolve("logs").resolve("2026-11.log") goes two levels down; on Java 8 to 21 chaining is the way to add several names one by one.
Immutable base.resolve returns new objects, so app still prints myapp at the end; nothing was created on disk either, which Files.createDirectories would do.
Example 3 — See That resolve() Keeps ".." Until You Normalize
Join a child that climbs out of the folder, clean it with normalize(), and check whether it is still inside the reports folder.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathResolveDotsKept {
public static void main(String[] args) {
Path reports = Paths.get("company/reports");
Path joined = reports.resolve("../secrets/salaries.csv");
System.out.println("resolved: " + joined);
System.out.println("normalized: " + joined.normalize());
System.out.println("inside reports? " + joined.normalize().startsWith(reports));
Path a = reports.resolve("2026/q1.csv");
Path b = reports.resolve(Paths.get("2026", "q1.csv"));
System.out.println("String and Path overloads equal: " + a.equals(b));
}
}
Output
Run with java PathResolveDotsKept. Shown for Linux and macOS. Windows prints the same paths with \ between the names.