In Java, path.toString() returns the path as a String. Java calls it automatically when you print a path or join it with +. The text uses the separator of the running system: docs/reports/jan.txt on Linux and macOS, docs\reports\jan.txt on Windows.
The text is the cleaned form that was stored when the path was created: repeated and trailing separators are gone, but . and .. are still there. Paths.get(path.toString()) gives an equal path back.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.toString(...), after import java.nio.file.Path;.
Syntax
Call it directly, or let printing and string concatenation call it:
Java
Path report = Paths.get("docs", "reports", "jan.txt");
String text = report.toString(); // "docs/reports/jan.txt" on Linux/macOS
System.out.println("File: " + report); // calls toString() for you
Call
What it does
p.toString()
The path text with the native separator.
"" + p, String.valueOf(p)
The same text; valueOf gives "null" for a null path.
p.toString().replace('\\', '/')
Forward slashes on every system, for URLs or ZIP entries.
Since Java 7. For a text form that is portable between systems and programs, prefer toUri().
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 — Turn a Path into a String
Convert a report path explicitly, implicitly with +, and with String.valueOf, and check the length of the text.
One separator per gap. The three parts are joined with the native separator, so the 18 characters of the names plus two separators give a String of length 20 on every system.
Concatenation calls it for you."concat: " + report invokes toString() automatically, so printing a path never needs the explicit call.
valueOf gives the same text.String.valueOf(report) also calls toString(); it only differs when the path is null.
Example 2 — See How the Text Is Cleaned When a Path Is Created
Print the text of paths typed with trailing, doubled, and leading separators, and with a leading dot.
Java
import java.nio.file.Paths;
public class PathToStringCleaned {
public static void main(String[] args) {
String[] inputs = {"docs/", "docs//a.txt", "./docs/a.txt"};
for (String s : inputs) {
System.out.println("[" + s + "] -> [" + Paths.get(s).toString() + "]");
}
System.out.println("parts with slashes -> [" + Paths.get("docs/", "/a.txt").toString() + "]");
}
}
Output
Run with java PathToStringCleaned. Shown for Linux and macOS. On Windows the right-hand side uses \, for example [docs\a.txt] and [.\docs\a.txt].
[docs/] -> [docs]
[docs//a.txt] -> [docs/a.txt]
[./docs/a.txt] -> [./docs/a.txt]
parts with slashes -> [docs/a.txt]
How It Works
Trailing and doubled separators go. docs/ becomes docs and docs//a.txt becomes docs/a.txt, because the path is stored in a cleaned form as soon as it is parsed.
Dots are kept. ./docs/a.txt prints with its leading dot; only normalize() removes . and ...
Joining parts is forgiving.Paths.get("docs/", "/a.txt") would produce three separators in a row, but the stored text has just one.
Example 3 — Print Empty and Missing Paths Safely
The empty path prints as an empty String, and a missing parent is null. Compare three ways of turning them into text.
The empty path has empty text.Paths.get("") is a valid path whose toString() is "", so isEmpty() on the text is the simplest test for it.
null is not a path. notes.txt has no parent, so parent is null; String.valueOf prints the word null, and Objects.toString lets you choose a friendlier fallback.
Direct calls on null fail.parent.toString() has no object to run on, so it throws NullPointerException; use one of the null-safe forms when a path may be missing.
Frequently Asked Questions
Call path.toString(), or simply concatenate the path with a String. Both give the path text with the native separator.
On Windows the separator is \, and toString() always uses the native separator. Replace it with / when you need a URL or a ZIP entry name.
No. It returns the stored text. Repeated slashes are already removed when the path is created, but "." and ".." stay until you call normalize().
The empty string "". Paths.get("").toString().isEmpty() is true.
Use Paths.get(text), or Path.of(text) on Java 11 and later.