Open In App

Path relativize() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The relativize(Path other) method of java.nio.file.Path used to create a relative path between this path and a given path as a parameter. Relativization is the inverse of resolution. This method creates a relative path that when resolved against this path object, yields a path that helps us to locate the same file as the given path. For example, if this path is “/dir1/dir2” and the given path as a parameter is “/dir1/dir2/dir3/file1” then this method will construct a relative path “dir3/file1”. Where this path and the given path do not have a root component, then a relative path can be constructed. If anyone of the paths has a root component then the relative path cannot be constructed. When both paths have a root component then it is implementation-dependent if a relative path can be constructed. If this path and the given path are equal then an empty path is returned. 

Syntax:

Path relativize(Path other)

Parameters: This method accepts a one parameter other which is the path to relativize against this path. 

Return value: This method returns the resulting relative path, or an empty path if both paths are equal. 

Exception: This method throws IllegalArgumentException if other is not a Path that can be relativized against this path. 

Below programs illustrate relativize() method: 

Program 1: 

Java




// Java program to demonstrate
// java.nio.file.Path.relativize() method
 
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
    {
 
        // create objects of Path
        Path path
            = Paths.get("D:\\eclipse\\p2"
                        + "\\org\\eclipse");
        Path passedPath
            = Paths.get("D:\\eclipse\\p2"
                        + "\\org\\eclipse\\equinox\\p2\\core"
                        + "\\cache\\binary");
 
        // print paths
        System.out.println("This Path:"
                           + path);
        System.out.println("Given Path:"
                           + passedPath);
 
        // call relativize() to create
        // a relative path
        Path relativize
            = path.relativize(passedPath);
 
        // print result
        System.out.println("Relative Path: "
                           + relativize);
    }
}


Output:

Program 2: 

Java




// Java program to demonstrate
// java.nio.file.Path.relativize() method
 
import java.nio.file.Path;
import java.nio.file.Paths;
public class GFG {
    public static void main(String[] args)
    {
 
        // create objects of Path
        Path path
            = Paths.get("\\nEclipseWork");
        Path passedPath
            = Paths.get("\\nEclipseWork\\GFG"
                        + "\\bin\\defaultpackage");
 
        // print paths
        System.out.println("This Path:"
                           + path);
        System.out.println("Given Path:"
                           + passedPath);
 
        // call relativize()
        // to create a relative path
        Path relativize
            = path.relativize(passedPath);
 
        // print result
        System.out.println("Relative Path: "
                           + relativize);
    }
}


Output:

References: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#relativize(java.nio.file.Path)



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