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
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
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:
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:
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:
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:
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:
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.
Related reading
- How to compare the performance of Android Apps written in Java and Xamarin C#? Anyway to check quantitative data (code & results)
- How to compress pointer ? eg. arbitrary bit pointer
- How to compute intersection of N sorted sets?
- How to compute the absolute minimum amount of changes to convert one sortorder into another?
- How to compare dates in Java?
- How to compare two ListString to each other?
- How to configure a fine tuned thread pool for futures?
- How to configure Apache Tika in a kube environment to obtain maximum throughput when parsing a massive number of documents?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

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 courseData 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.