Path resolveSibling() method in Java with Examples

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

There are two types of resolveSibling() methods.

  1. resolveSibling(Path other) method of java.nio.file.Path used to resolve the given path as parameter against this path’s parent path. This is very useful where a file name needs to be replaced with another file name. For example, suppose that the name separator is “/” and a path represents “drive/newFile/spring”, then invoking this method with the Path “plugin” will result in the Path “drive/newFile/spring/plugin”. If this path does not have a parent path or other is absolute, then this method returns others. If other is an empty path then this method returns this path’s parent, or where this path doesn’t have a parent, the empty path.



    Syntax:

    default Path resolveSibling(Path other)
    

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

    Return value: This method return the resulting path.

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // Path.resolveSibling(Path other) method
      
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class GFG {
        public static void main(String[] args)
        {
      
            // create 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 resolveSibling()
            // to create resolved Path
            // on parent of path object
            Path resolvedPath
                = path.resolveSibling(path2);
      
            // print result
            System.out.println("Resolved Path "
                               + "of path's parent:"
                               + resolvedPath);
        }
    }

    chevron_right

    
    

    Output:
  2. resolveSibling(String other) method of java.nio.file.Path used to converts a given path string which we passed as parameter to a Path and resolves it against this path’s parent path in exact same manner as specified by the resolveSibling method.

    Syntax:

    default Path resolveSibling(String other)
    

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

    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 resolveSibling(String other) method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // Path.resolveSibling(String other) method
      
    import java.nio.file.Path;
    import java.nio.file.Paths;
    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 resolveSibling()
            // to create resolved Path
            // on parent of path object
            Path resolvedPath
                = path.resolveSibling(passedPath);
      
            // print result
            System.out.println("Resolved Path of "
                               + "path's parent:"
                               + 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.