Java
Programming
Code Optimization
Path Combination
Software Development

How to combine (join) paths in Java with Path.of and resolve

Codemia Knowledge Hub ·

Combine paths in Java with the java.nio.file.Path API: build one from segments with Path.of(...) (or Paths.get(...)), and attach a child to an existing base with base.resolve(child). Avoid joining strings with slashes. Path handles platform separators, absolute-path rules and normalization, and plugs straight into file operations.

Build a path from segments with Path.of

java
1import java.nio.file.Path;
2
3Path path = Path.of("var", "data", "images", "photo.jpg");
4System.out.println(path);

Older code uses Paths.get(...), which is fine too. Both let Java pick the correct separator for the host platform.

Join a child onto a base with resolve

java
1import java.nio.file.Path;
2
3Path base = Path.of("/srv/uploads");
4Path file = base.resolve("avatars/user.png");
5
6System.out.println(file);

This is the path-composition method you will use most in real code.

An absolute child replaces the base

If the child path is absolute, resolve ignores the base and returns the child:

java
1import java.nio.file.Path;
2
3Path base = Path.of("/srv/uploads");
4Path child = Path.of("/tmp/file.txt");
5
6System.out.println(base.resolve(child));

That is correct behavior, and it is why resolve is not a simple string join.

Clean up "." and ".." with normalize

normalize removes redundant "." and ".." segments lexically:

java
1import java.nio.file.Path;
2
3Path messy = Path.of("/srv/uploads/./avatars/../avatars/user.png");
4Path normalized = messy.normalize();
5
6System.out.println(normalized);

This is handy before logging or comparing paths. It does not check that the path exists and does not follow symlinks. For a canonical path that resolves symlinks and requires the file to exist, use toRealPath() and handle the checked exception it can throw.

Keep user-supplied paths inside a base directory

When a user provides part of the path, normalize the result and verify it is still under the intended base:

java
1import java.nio.file.Path;
2
3Path base = Path.of("/srv/uploads").toAbsolutePath().normalize();
4Path target = base.resolve("images/user.png").normalize();
5
6if (!target.startsWith(base)) {
7    throw new IllegalArgumentException("path traversal attempt");
8}

Use this pattern in upload, download and file-serving endpoints.

Put a file next to another with resolveSibling

To create a path beside an existing file rather than inside it, resolveSibling avoids manual parent handling:

java
1import java.nio.file.Path;
2
3Path report = Path.of("/srv/reports/daily.csv");
4Path backup = report.resolveSibling("daily.bak");
5
6System.out.println(backup);

It suits companion files such as backups, temporary files or sidecar metadata in reporting and export code, and it makes the intent obvious.

Use Path directly with Files

Path objects work naturally with Files for parent lookup, directory creation and writing:

java
1import java.nio.file.Files;
2import java.nio.file.Path;
3
4Path output = Path.of("build", "reports", "daily.txt");
5Files.createDirectories(output.getParent());
6Files.writeString(output, "done");

Stay with Path from the start instead of switching between strings and file APIs. Manual concatenation such as base + "/" + child bypasses platform-aware separators and ignores absolute-path semantics.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms