Skip to content
Related Articles

Related Articles

Improve Article

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:

MethodDescription

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:

  1. first – initial part of the Path.
  2. 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);
    }
}
Output
/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:

  1. IllegalArgumentException – if the parameter of URI is not appropriate
  2. FileSystemNotFoundException – if the file system, which is identified by URI does not exist
  3. 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
    {
        String uribase = "https://www.geeksforgeeks.org/";
        // 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/

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :