Thursday, September 4, 2025
HomeLanguagesJavaPath relativize() method in Java with Examples

Path relativize() method in Java with Examples

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)

RELATED ARTICLES

Most Popular

Dominic
32262 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS