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);
Call
What 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
Parameter
Description
(none)
No arguments.
Return Value
Return Value
Description
java.net.URI
An absolute, hierarchical URI with the scheme file for the default file system.
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());
}
}
Output
Run with java PathToUriBasic. Shown for Linux and macOS. On Windows Java adds the drive of the working directory, for example file:///C:/home/anna/My%20Docs/report.txt and /C:/home/anna/My Docs/report.txt.
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:///.
Spaces are encoded. URIs cannot contain spaces, so My Docs becomes My%20Docs in the URI text.
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()));
}
}
Output
Run with java PathToUriRelative. The output is the same on Windows, macOS, and Linux.
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.
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).
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:///"));
}
}
Output
Run with java PathToUriEncoding. The output is the same on Windows, macOS, and Linux; only the part of the URI before the file name differs on Windows.
last part: a%20b%231.txt
decoded name: a b#1.txt
Path.toUri uses file:/// true
File.toURI uses file:/// false
How It Works
Two characters need codes. A space becomes %20 and #, which would otherwise start a URI fragment, becomes %23, giving a%20b%231.txt.
The name survives the round trip.Paths.get(uri) decodes the URI, so the file name is a b#1.txt again.
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.