Java Program to Get the relative path from two absolute paths

Example 1: Get a relative path from two absolute paths using URI class

import java.io.File;
import java.net.URI;

class Main {
  public static void main(String[] args) {

    try {

      // Two absolute paths
      File absolutePath1 = new File("C:\\Users\\Desktop\\Programiz\\Java\\Time.java");
      System.out.println("Absolute Path1: " + absolutePath1);
      File absolutePath2 = new File("C:\\Users\\Desktop");
      System.out.println("Absolute Path2: " + absolutePath2);

      // convert the absolute path to URI
      URI path1 = absolutePath1.toURI();
      URI path2 = absolutePath2.toURI();

      // create a relative path from the two paths
      URI relativePath = path2.relativize(path1);

      // convert the URI to string
      String path = relativePath.getPath();

      System.out.println("Relative Path: " + path);


    } catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output

Absolute Path1: C:\Users\Desktop\Programiz\Java\Time.java
Absolute Path2: C:\Users\Desktop
Relative Path: Programiz/Java/Time.java

In the above example, we have two absolute paths named absolutePath1 and absolutePath2. We have used the URI class to convert the absolute paths into the relative path.

  • toURI() - converts the File object to a Uri
  • relativize() - extracts the relative path by comparing two absolute paths with one another
  • getPath() - converts the Uri into a string

Also Read:


Example 2: Get a relative path from two absolute path using String methods

import java.io.File;

class Main {
  public static void main(String[] args) {

    // Create file objects
    File file1 = new File("C:\\Users\\Desktop\\Programiz\\Java\\Time.java");
    File file2 = new File("C:\\Users\\Desktop");

    // convert file objects to string
    String absolutePath1 = file1.toString();
    System.out.println("Absolute Path1: " + absolutePath1);
    String absolutePath2 = file2.toString();
    System.out.println("Absolute Path2: " + absolutePath2);

    // get the relative path
    String relativePath = absolutePath1.substring(absolutePath2.length());
    System.out.println("Absolute Path: " + relativePath);
  }
}

Output

Absolute Path1: C:\Users\Desktop\Programiz\Java\Time.java
Absolute Path2: C:\Users\Desktop
Absolute Path: \Programiz\Java\Time.java

In the above example, we have converted the file paths to strings. Notice the expression,

absolutePath1.substring(absolutePath2.length())

Here, the substring() method returns the part of absolutePath1 starting from index equal to the length of absolutePath2. That is, the string represented by absolutePath2 is removed from absolutePath1.

To learn more about how substring works, visit Java String substring().


Example 3: Get a relative path from two absolute paths using java.nio.file package

import java.nio.file.Path;
import java.nio.file.Paths;

class Main {
  public static void main(String[] args) {

    // Create file objects
    Path absolutePath1 = Paths.get("C:\\Users\\Desktop\\Programiz\\Java\\Time.java");
    Path absolutePath2 = Paths.get("C:\\Users\\Desktop");

    // convert the absolute path to relative path
    Path relativePath = absolutePath2.relativize(absolutePath1);
    System.out.println("Relative Path: " + relativePath);

  }
}

Output

Relative Path: Programiz\Java\Time.java

In the above example, we have used the relativize() method of the Path interface to get a relative path from two absolute paths.

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges