Java Path toUri() Method

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

Definition

In Java, path.toUri() returns a java.net.URI that identifies the file, such as file:///home/anna/My%20Docs/report.txt. The URI is always absolute: a relative path is first completed with the working directory, as toAbsolutePath() would do.

Characters that are not allowed in a URI, such as spaces or #, are percent-encoded. A URI is a portable way to store or share a location; Paths.get(uri) (or Path.of(uri) on Java 11+) turns it back into a path.

An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.toUri(...), after import java.nio.file.Path;.

Syntax

Call it with no arguments; import java.net.URI to keep the result:

Java
URI uri = Paths.get("/home/anna/My Docs/report.txt").toUri();
// file:///home/anna/My%20Docs/report.txt on Linux and macOS
Path back = Paths.get(uri);
CallWhat it does
p.toUri()Absolute file:/// URI with percent-encoding.
p.toUri().getPath()The decoded path part of the URI.
Paths.get(uri)From the URI back to an (absolute) path.

Since Java 7. If the path names an existing directory, the URI ends with a slash, so the exact text can depend on the disk.

Library declarations
Java
// java.nio.file.Path (interface)
URI toUri()

Parameters

ParameterDescription
(none)No arguments.

Return Value

Return ValueDescription
java.net.URIAn absolute, hierarchical URI with the scheme file for the default file system.

Compare with Similar Methods

Other ways to describe a location:

MethodHow it differs
toString()Native text, may be relative, no encoding.
Path.of(URI)The reverse conversion (Java 11+); Paths.get(URI) on Java 8.
toAbsolutePath()The same completion step, but the result stays a Path.

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 an Absolute Path into a file: URI

Convert a path with a space in a folder name and look at the whole URI, its scheme, and its decoded path.

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

public class PathToUriBasic {
    public static void main(String[] args) {
        Path report = Paths.get("/home/anna/My Docs/report.txt");
        URI uri = report.toUri();
        System.out.println("URI:    " + uri);
        System.out.println("scheme: " + uri.getScheme());
        System.out.println("path:   " + uri.getPath());
    }
}

How It Works

  1. Three slashes. file: is the scheme, the empty authority between // and the next / means "this computer", and the path follows, which is why the URI starts with file:///.
  2. Spaces are encoded. URIs cannot contain spaces, so My Docs becomes My%20Docs in the URI text.
  3. getPath() decodes again. The URI keeps the original characters internally, so uri.getPath() shows the readable path with the space.

Example 2 — Convert a Relative Path to a URI and Back

A relative path becomes a full URI. Check its properties and the round trip without printing the machine-specific folder.

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

public class PathToUriRelative {
    public static void main(String[] args) {
        Path rel = Paths.get("report.txt");
        URI uri = rel.toUri();
        System.out.println("scheme:          " + uri.getScheme());
        System.out.println("absolute URI:    " + uri.isAbsolute());
        System.out.println("ends with name:  " + uri.toString().endsWith("/report.txt"));
        System.out.println("round trip:      " + Paths.get(uri).equals(rel.toAbsolutePath()));
    }
}

How It Works

  1. Completed first. report.txt is relative, so toUri() puts the working directory in front before building the URI; the result always has a scheme and is absolute.
  2. Check the end, not the start. The beginning of the URI depends on the computer, but it reliably ends with /report.txt (as long as no folder of that name exists).
  3. Back to an absolute path. Paths.get(uri) returns the absolute path, so it equals rel.toAbsolutePath(), not the relative rel itself.

Example 3 — See How Special Characters Are Encoded

A file name with a space and a # must be encoded in a URI. Compare the result of Path.toUri() with the older File.toURI().

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

public class PathToUriEncoding {
    public static void main(String[] args) {
        Path p = Paths.get("/srv/files/a b#1.txt");
        URI uri = p.toUri();
        String text = uri.toString();
        System.out.println("last part:    " + text.substring(text.lastIndexOf('/') + 1));
        System.out.println("decoded name: " + Paths.get(uri).getFileName());
        System.out.println("Path.toUri uses file:///  " + text.startsWith("file:///"));
        System.out.println("File.toURI uses file:///  " + p.toFile().toURI().toString().startsWith("file:///"));
    }
}

How It Works

  1. Two characters need codes. A space becomes %20 and #, which would otherwise start a URI fragment, becomes %23, giving a%20b%231.txt.
  2. The name survives the round trip. Paths.get(uri) decodes the URI, so the file name is a b#1.txt again.
  3. Two valid styles. Path.toUri() writes file:/// with an empty authority, while File.toURI() writes the short form file:/; compare URI objects or paths, not this text.

Frequently Asked Questions

Call path.toUri(). It returns an absolute file:/// URI with special characters percent-encoded.
Use Paths.get(uri) on Java 7 and later, or Path.of(uri) on Java 11 and later.
URIs are always absolute, so toUri first completes the path with the working directory, just like toAbsolutePath.
Path.toUri writes file:///path, while File.toURI writes file:/path. Both describe the same file.
toUri checks the disk, and if the path is an existing directory it adds a trailing slash, as is usual for folder URIs.

Back to the method index

See all 23 Path method guides grouped by what they do.

Method index →

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.

11 people found this page helpful