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));
}
}