java.nio.file.Paths class contains static methods for converting path string or URI into Path.
Class declaration :
public final class Paths extends Object
Methods:
Method | Description |
---|---|
get(String first, String… more) |
This method converts a path string, or a sequence of strings that when joined form a path string, to a Path. |
get(URI uri) |
This method converts the given URI to a Path object. |
1. public static Path get(String first, String… more):
Returns a Path by converting given strings into a Path. If “more” doesn’t specify any strings than “first” is the only string to convert. If “more” specify extra strings then “first” is the initial part of the sequence and the extra strings will be appended to the sequence after “first” separated by “/”.
Parameters:
- first – initial part of the Path.
- more – extra strings to be joined to the Path.
Returns: resulting Path
Throws:
InvalidPathException – if a given string cannot be converted to a Path
Java
// Java program to demonstrate // java.nio.file.Path.get(String first,String... more) // method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public static void main(String[] args) throws IOException { // create object of Path Path path = (Path)Paths.get( "/usr" , "local" , "bin" ); // print Path System.out.println(path); } } |
/usr/local/bin
2.public static Path get(URI uri): Returns a Path by converting given Uri into a Path.
Parameters:
- uri – to be converted
Returns: resulting Path
Throws:
- IllegalArgumentException – if the parameter of URI is not appropriate
- FileSystemNotFoundException – if the file system, which is identified by URI does not exist
- SecurityException – if security manager denies access to file system
Java
// Java program to demonstrate // java.nio.file.Path.get(URI uri) method import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.nio.file.Paths; public class Path { public static void main(String[] args) throws IOException, URISyntaxException { // Constructor to create a new URI // by parsing the string URI uri = new URI(uribase); // create object of Path Path path = (Path)Paths.get(uri); // print ParentPath System.out.println(path); } } |
Output:
https://www.geeksforgeeks.org/