Open In App

Path resolveSibling() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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/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:




    // 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("programs\\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);
        }
    }

    
    

    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:




    // 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);
        }
    }

    
    

    Output:

References:



Last Updated : 11 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads