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/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:
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
public static void main(String[] args)
{
Path path
= Paths.get( "drive\\temp\\Spring" );
Path path2
= Paths.get( "programs\\workspace" );
Path resolvedPath
= path.resolveSibling(path2);
System.out.println( "Resolved Path "
+ "of path's parent:"
+ resolvedPath);
}
}
|
Output:
- 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:
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
public static void main(String[] args)
{
Path path
= Paths.get( "drive\\temp\\Spring" );
String passedPath = "drive" ;
Path resolvedPath
= path.resolveSibling(passedPath);
System.out.println( "Resolved Path of "
+ "path's parent:"
+ resolvedPath);
}
}
|
Output:
References:
Last Updated :
11 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...