Path and Paths.get()
Fast recall
Quick revision
Core answer
Path is an immutable, provider-specific representation of a hierarchical path. Create one with Path.of(...) or the older Paths.get(...); creating a Path is normally syntactic and does not create or verify a file.Interview-ready guide
Interview answer
java.nio.file.Path is an immutable representation of a path in a file system. It is made of ordered name elements and may be absolute, such as a rooted operating-system path, or relative to some later base. A Path is a value used by the NIO file APIs; constructing one does not create a file and normally does not check whether anything exists there.
Modern Java code creates default-file-system paths with Path.of("logs", "app.log"). Paths.get(...) remains valid and delegates to Path.of. A file: URI can also be converted with Path.of(uri). Other providers, such as a ZIP file system, can produce Paths with provider-specific rules, so code should not build paths by manual string concatenation.
Path offers structural operations such as getFileName, getParent, getNameCount, resolve, relativize, and normalize. These mostly work on path syntax. toAbsolutePath() creates an absolute form, commonly using the current working directory for a relative path, but still does not promise that the target exists.
For example, Path.of("reports").resolve("daily.txt") represents reports/daily.txt using the platform's path rules. The program can pass it to Files.createDirectories, Files.newBufferedWriter, or another operation when it is ready to access storage.
toRealPath() is different: it performs file-system I/O, requires the target to exist, removes redundant elements, and resolves symbolic links by default. Because links and permissions affect the result, it can throw IOException. Use ordinary Path operations to describe locations; use Files or toRealPath only when the application intentionally needs an observed file-system fact.
Path and Paths.get()
Direct Answer
Path is an immutable, provider-specific representation of a hierarchical path. Create one with Path.of(...) or the older Paths.get(...); creating a Path is normally syntactic and does not create or verify a file.
Deep Dive & Architecture
Separating a path value from a file resource is one of NIO's most useful ideas. A Path can be parsed, compared, stored, and combined before a file is created. The actual I/O begins when it is passed to operations such as Files.readAttributes, Files.newInputStream, or toRealPath. That distinction also explains why a Path may identify a target that never exists.
Path rules come from its file-system provider. Separators, roots, case sensitivity, valid characters, and URI conversion are not universal string rules. Two Paths from different providers cannot necessarily be resolved or relativized together. Keeping values as Path objects lets the provider enforce its own semantics.
toAbsolutePath and toRealPath answer different questions. The first produces an absolute spelling, which may still contain a nonexistent target or symbolic links. The second asks the file system for a canonicalized existing location and can change as links or the file system change. It is an I/O observation, not a permanent identity token.
Security-sensitive code must also account for time. Checking a path and then opening it creates a gap in which another actor may replace a link or entry. Where the threat model includes such races, use constrained directories, appropriate provider APIs, permissions, and handle-based designs rather than treating one earlier toRealPath result as an everlasting guarantee.
Code Example
import java.nio.file.Path;
class PathCreation {
public static void main(String[] args) {
Path relative = Path.of("reports", "..", "reports", "daily.txt");
Path normalized = relative.normalize();
if (!normalized.endsWith(Path.of("reports", "daily.txt"))) {
throw new AssertionError(normalized);
}
System.out.println(normalized);
}
}
Check your understanding
Answer these without looking back at the article.
- Does Path.of create or validate a file?
- How do toAbsolutePath and toRealPath differ?
- Why should path components not be joined with hard-coded separators?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.