Path resolve() method in Java with Examples

07 Apr, 2022
resolve() method of java.nio.file.Path used to resolve the given path against this path.
There are two types of resolve() methods.
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:// 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);    }}Output: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 separator. 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:// 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("programs\\workspace");          // call resolve()        // to create resolved Path        Path resolvedPath            = path.resolve(path2);          // print result        System.out.println("Resolved Path:"                           + resolvedPath);    }}Output:
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:// 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);    }}Output:
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:
// 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);    }}
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 separator. 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:// 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("programs\\workspace");          // call resolve()        // to create resolved Path        Path resolvedPath            = path.resolve(path2);          // print result        System.out.println("Resolved Path:"                           + resolvedPath);    }}Output:
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:
// 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("programs\\workspace");          // call resolve()        // to create resolved Path        Path resolvedPath            = path.resolve(path2);          // print result        System.out.println("Resolved Path:"                           + resolvedPath);    }}
References:
https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#resolve(java.lang.String)
https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#resolve(java.nio.file.Path)
sweetyty
kashishsoda
Java-Functions
Java-Path
java.nio.file package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.