java.nio.file.Paths Class in Java
Last Updated :
12 Mar, 2021
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
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
{
Path path = (Path)Paths.get( "/usr" , "local" , "bin" );
System.out.println(path);
}
}
|
2.public static Path get(URI uri): Returns a Path by converting given Uri into a Path.
Parameters:
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
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
{
URI uri = new URI(uribase);
Path path = (Path)Paths.get(uri);
System.out.println(path);
}
}
|
Output:
https://www.geeksforgeeks.org/
Share your thoughts in the comments
Please Login to comment...