Close

Java 11 - java.nio.file.Path Changes

[Last Updated: Sep 30, 2018]

Java 11 added two new overloaded static methods in java.nio.file to conveniently create a Path instance.

Creating Path from a sequence of strings

public static Path of(String first, String... more)
public class PathOfExample {
  public static void main(String[] args) {
      Path path = Path.of("C:", "temp", "test.txt");
      System.out.println(path);
      boolean exists = Files.exists(path);
      System.out.println(exists);
  }
}
C:\temp\test.txt
true

Creating Path from a URI

public static Path of(URI uri)
public class PathOfUriExample {
  public static void main(String[] args) {
      URI uri = URI.create("file:///C:/temp/test.txt");
      System.out.println(uri);
      Path path = Path.of(uri);
      System.out.println(path);
      System.out.println(Files.exists(path));
  }
}
file:///C:/temp/test.txt
C:\temp\test.txt
true

Example Project

Dependencies and Technologies Used:

  • JDK 9.0.1
Java 11 - java.nio.file.Path Changes Select All Download
  • java-11-path-changes
    • src
      • com
        • logicbig
          • example
            • PathOfExample.java

    See Also