In Java, java.nio.file.Path is an interface whose objects describe where a file or folder is, like an address on an envelope. It arrived in Java 7 with the "NIO.2" file API. A path is made of an optional root (such as / or C:\) and a list of name elements: /home/anna/notes.txt has the root / and the names home, anna, and notes.txt.
Windows prints \ between names and needs a drive letter for an absolute path; macOS and Linux print /. Method pages show the Linux and macOS output and add a Windows note where the result really differs.
Syntax
Java Syntax
Import Path and create it with Paths.get (Java 7+) or Path.of (Java 11+):
Java
import java.nio.file.Path;
import java.nio.file.Paths;
Path config = Paths.get("config", "app.properties"); // config/app.properties
Path same = Path.of("config/app.properties"); // Java 11+, equal to config
Path home = Paths.get("/home/anna"); // absolute on Linux/macOS
Path file = home.resolve("todo.txt"); // /home/anna/todo.txt
String text = file.toString(); // the path as a String
Illegal characters cause an unchecked InvalidPathException. On Linux and macOS only the NUL character is illegal; Windows also rejects characters such as < > : " | ? * inside a name.
Reference
Path vs File vs String
Three ways to hold a location, from oldest to newest:
Type
What it is
Use it for
String
Plain text such as "docs/a.txt"
Input and output only; joining strings by hand breaks on other systems
java.io.File
The Java 1.0 file class
Older APIs; many methods just return false on failure
An absolute path is complete on its own: /home/anna/todo.txt on Linux or C:\Users\anna\todo.txt on Windows. A relative path such as docs/todo.txt is read from the program's working directory. isAbsolute() tells them apart, getRoot() returns the root (or null), and toAbsolutePath() turns a relative path into an absolute one. Name elements are numbered from 0 without the root: getNameCount(), getName(), subpath(), and iterator() work with them.
Print the root, the parent folder, the file name, and every name element of one absolute path. No file has to exist.
Java
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathHubAnatomy {
public static void main(String[] args) {
Path report = Paths.get("/home/anna/reports/2026/summary.pdf");
System.out.println("Root: " + report.getRoot());
System.out.println("Parent: " + report.getParent());
System.out.println("File: " + report.getFileName());
System.out.println("Absolute: " + report.isAbsolute());
int i = 0;
for (Path part : report) {
System.out.println(" name " + i + " = " + part);
i++;
}
}
}
Output
Run with java PathHubAnatomy. Shown for Linux and macOS. On Windows the root prints as \, the parent as \home\anna\reports\2026, and isAbsolute() is false, because a path without a drive letter is not absolute there.
Root: /
Parent: /home/anna/reports/2026
File: summary.pdf
Absolute: true
name 0 = home
name 1 = anna
name 2 = reports
name 3 = 2026
name 4 = summary.pdf
How It Works
Only a name.Paths.get parses the text into a root and five name elements; Java does not check whether /home/anna exists.
Three quick questions.getRoot(), getParent(), and getFileName() each return a new Path, and string concatenation prints it with toString().
A Path is Iterable. The for-each loop visits the name elements from the root side outward; the root itself is not one of them, so index 0 is home.
Example 2 — Build a Log Path from Parts and Clean It
Join a project folder with sub-folders and a file name, then remove a .. step and compute a relative path back to the project.
An interface, added in Java 7 as part of NIO.2, whose objects describe the location of a file or folder as a root plus a list of names. A Path is immutable and does not need to exist on disk.
Use Path with the Files class for new code. It reports errors with clear exceptions and supports links, attributes, and other file systems. Convert with path.toFile() and file.toPath() when an older API needs a File.
They create the same kind of Path. Paths.get works on Java 7 and later; Path.of was added in Java 11 and is the recommended style there. The examples on this site use Paths.get so they run on Java 8.
Each operating system has its own separator. Windows uses a backslash and macOS and Linux use a slash. Java accepts forward slashes in Paths.get on Windows too, but toString always prints the native separator.
No. Paths.get and Path.of only parse text. Use Files.createFile, Files.createDirectories, or Files.write to make something on disk, and Files.exists to check it.