Java Path toString() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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
CallWhat 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().

Library declarations
Java
// java.nio.file.Path (interface)
String toString()

Parameters

ParameterDescription
(none)No arguments.

Return Value

Return ValueDescription
StringThe path text; the empty string for the empty path.

Compare with Similar Methods

Other text forms of a path:

MethodHow it differs
toUri()An absolute file:/// URI with percent-encoding.
toFile().getPath()The same text, through the old java.io.File class.
getFileName().toString()Only the last name as text.

Java Examples

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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathToStringBasic {
    public static void main(String[] args) {
        Path report = Paths.get("docs", "reports", "jan.txt");
        String text = report.toString();
        System.out.println("toString(): " + text);
        System.out.println("length:     " + text.length());
        System.out.println("concat:     " + report);
        System.out.println("valueOf equal: " + String.valueOf(report).equals(text));
    }
}

How It Works

  1. 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.
  2. Concatenation calls it for you. "concat: " + report invokes toString() automatically, so printing a path never needs the explicit call.
  3. 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() + "]");
    }
}

How It Works

  1. 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.
  2. Dots are kept. ./docs/a.txt prints with its leading dot; only normalize() removes . and ...
  3. 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.

Java
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

public class PathToStringNullEmpty {
    public static void main(String[] args) {
        Path empty = Paths.get("");
        System.out.println("empty path: [" + empty.toString() + "], isEmpty: " + empty.toString().isEmpty());
        Path parent = Paths.get("notes.txt").getParent();
        System.out.println("String.valueOf: " + String.valueOf(parent));
        System.out.println("Objects.toString: " + Objects.toString(parent, "(no folder)"));
        try {
            System.out.println(parent.toString());
        } catch (NullPointerException e) {
            System.out.println("parent.toString() -> " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. 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.
  2. 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.
  3. 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.

Next: Path.toUri()

Turn a path into an absolute file: URI, for example to share or store it.

toUri() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful