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.
-
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:// Java program to demonstrate// Path.resolveSibling(Path other) methodimportjava.nio.file.Path;importjava.nio.file.Paths;publicclassGFG {publicstaticvoidmain(String[] args){// create object of PathPath path= Paths.get("drive\\temp\\Spring");// create an object of Path// to pass to resolve methodPath path2= Paths.get("programes\\workspace");// call resolveSibling()// to create resolved Path// on parent of path objectPath resolvedPath= path.resolveSibling(path2);// print resultSystem.out.println("Resolved Path "+"of path's parent:"+ resolvedPath);}}chevron_rightfilter_noneOutput:
- 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) methodimportjava.nio.file.Path;importjava.nio.file.Paths;publicclassGFG {publicstaticvoidmain(String[] args){// create an object of PathPath path= Paths.get("drive\\temp\\Spring");// create a string objectString passedPath ="drive";// call resolveSibling()// to create resolved Path// on parent of path objectPath resolvedPath= path.resolveSibling(passedPath);// print resultSystem.out.println("Resolved Path of "+"path's parent:"+ resolvedPath);}}chevron_rightfilter_noneOutput:
References:
- https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#resolveSibling(java.lang.String)
- https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#resolveSibling(java.nio.file.Path)
Recommended Posts:
- Path getParent() method in Java with Examples
- Path compareTo() method in Java with Examples
- Path getNameCount() method in Java with Examples
- Path getFileName() method in Java with Examples
- Path relativize() method in Java with Examples
- Path toString() method in Java with Examples
- Path getRoot() method in Java with Examples
- Path isAbsolute() method in Java with Examples
- Path subpath() method in Java with Examples
- Path startsWith() method in Java with Examples
- Path hashCode() method in Java with Examples
- Path endsWith() method in Java with Examples
- Path toAbsolutePath() method in Java with Examples
- Path iterator() method in Java with Examples
- Path toUri() method in Java with Examples
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.

