Programming for fun and profit

Programming tutorials, problems, solutions. Always with code.

Java Path conversion

Java Path conversion

In this tutorial we’re going to show how to convert between Path, legacy File, and URI. Path conversion allows to effectively work with legacy libraries.

Sometimes, when working with legacy libraries, there is a need to convert from to java.nio.file.Path or other way around. Also, sometimes, there’s a path in URI format, which also can be transformed into Java 7 Path.

Path to/from File conversion:

Conversion from/to File using File.toPath() and Path.toFile() methods:

Path path = new File("/proc/version").toPath();
System.out.println("Path from File: " + path);

System.out.println("path.toFile(): " + path.toFile());


Path from File: /proc/version
path.toFile(): /proc/version

Path to/from URI conversion:

Conversion from/to URI using Paths.get(URI) and Path.toURI():

Path path = Paths.get("/proc/version");
System.out.println("path.toUri(): " + path.toUri());

Path path = Paths.get(URI.create("file:///proc/version"));
System.out.println("Path from URI: " + path);


path.toUri(): file:///proc/version
Path from URI: /proc/version


  • java.nio.file.Path
  • java.nio.file.Paths

Check out Java IO Tutorial to learn more about Java NIO!


Share with the World!