Path resolve() method in Java with Examples

resolve() method of java.nio.file.Path used to resolve the given path against this path.

There are two types of resolve() methods.

  1. resolve(String other) method of java.nio.file.Path used to converts a given path string to a Path and resolves it against this Path in the exact same manner as specified by the resolve method. For example, If the name separator is “/” and a path represents “c/drive/files”, then invoking this method with the path string “file1” will result in the Path “c/drive/files/file1”.

    Syntax:



    default Path resolve(String other)
    

    Parameters: This method accepts a single parameter other which is the path string to resolve against this path.

    Return value: This method returns the resulting path.

    Exception: This method throws InvalidPathException if the path string cannot be converted to a Path..

    Below programs illustrate resolve(String other) method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // Path.resolve(String other) method
      
    import java.nio.file.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create an object of Path
            Path path
                = Paths.get("drive\\temp\\Spring");
      
            // create a string object
            String passedPath = "drive";
      
            // call resolve() to create resolved Path
            Path resolvedPath
                = path.resolve(passedPath);
      
            // print result
            System.out.println("Resolved Path:"
                               + resolvedPath);
        }
    }

    chevron_right

    
    

    Output:
  2. resolve(Path other) method of java.nio.file.Path used to resolve the given path against this path. This method will going to connect both paths together.If this path is “C/temp” and passed path is “drive/newFile” then this method will add passed path in the end of this path and use “/” as a seperator. So resolved path will be “C/temp/drive/newFile”.

    If the other parameter is an absolute path then this method trivially returns other. If other is an empty path then this method trivially returns this path. Otherwise, this method considers this path to be a directory and resolves the given path against this path. In the simplest case, the given path does not have a root component, in which case this method joins the given path to this path and returns a resulting path that ends with the given path. Where the given path has a root component then the resolution is highly implementation dependent and therefore unspecified.

    Syntax:

    Path resolve(Path other)
    

    Parameters: This method accepts a single parameter other which is the path to resolve against this path.

    Return value: This method return the resulting path.

    Below programs illustrate resolve(Path other) method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // Path.resolve(Path other) method
      
    import java.nio.file.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create an object of Path
            Path path
                = Paths.get("drive\\temp\\Spring");
      
            // create an object of Path
            // to pass to resolve method
            Path path2
                = Paths.get("programes\\workspace");
      
            // call resolve()
            // to create resolved Path
            Path resolvedPath
                = path.resolve(path2);
      
            // print result
            System.out.println("Resolved Path:"
                               + resolvedPath);
        }
    }

    chevron_right

    
    

    Output:

References:



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.