English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Other Java topics

Java program to obtain a relative path from two absolute paths

   Comprehensive Java Examples

In this example, we will learn how to obtain a relative path from two absolute paths in Java using the String method, URI class, and java.nio.file package.

Example1: Obtain a relative path from two absolute paths using the 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\\w3codebox\Java\Time.java");
      System.out.println("Absolute path"1: " + absolutePath1);
      File absolutePath2 = new File("C:\\Users\\Desktop");
      System.out.println("Absolute path"2: " + absolutePath2);
      //Convert an absolute path to URI
      URI path1 = absolutePath1.toURI();
      URI path2 = absolutePath2.toURI();
      //Create a relative path from two paths
      URI relativePath = path2.relativize(path)1);
      //Convert URI to string
      String path = relativePath.getPath();
      System.out.println("Relative Path: " + path);
    } catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Absolute path1: C:\Users\Desktop\w3codebox\Java\Time.java
Absolute path2: C:\Users\Desktop
Relative Path: w3codebox/Java/Time.java

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

  • toURI() - Convert the File object toUri

  • relativize() - Extract the relative path by comparing two absolute paths

  • getPath() - Convert URI to string

Example2To obtain the relative path from two absolute paths using the String method

import java.io.File;
class Main {}}
  public static void main(String[] args) {
    //Create File Object
    File file1 = new File("C:\\Users\\Desktop\\w3codebox\Java\Time.java");
    File file2 = new File("C:\\Users\\Desktop");
    // Convert the file object to a string
    String absolutePath1 = file1.toString();
    System.out.println("Absolute path"1: " + absolutePath1);
    String absolutePath2 = file2.toString();
    System.out.println("Absolute path"2: " + absolutePath2);
    //Get the relative path
    String relativePath = absolutePath1.substring(absolutePath2.length());
    System.out.println("Absolute path: " + relativePath);
  }
}

Output Result

Absolute path1: C:\Users\Desktop\w3codebox\Java\Time.java
Absolute path2: C:\Users\Desktop
Absolute path: \w3codebox\Java\Time.java

In the above example, we convert the file path to a string. Note the expression

absolutePath1.substring(absolutePath2.length())

Here, the substring () method returns absolutePath1A part of it, starting from the index equal to absolutePath2Starting from the length of the string. That is, from absolutePath1Delete absolutePath2 The string represented by

For more information on how substrings work, please visitJava String substring().

Example3To obtain the relative path from two absolute paths using the java.nio.file package

import java.nio.file.Path;
import java.nio.file.Paths;
class Main {}}
  public static void main(String[] args) {
    //Create File Object
    Path absolutePath1 = Paths.get("C:\\Users\\Desktop\\w3codebox\Java\Time.java");
    Path absolutePath2 = Paths.get("C:\\Users\\Desktop");
    //Convert Absolute Path to Relative Path
    Path relativePath = absolutePath2.relativize(absolutePath1);
    System.out.println("Relative Path: " + relativePath);
  }
}

Output Result

Relative Path: w3codebox\Java\Time.java

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

Comprehensive Java Examples