Learn Java by Example: Java Program to Get the relative path from two absolute paths

Java Program to Get the relative path from two absolute paths

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

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\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:UsersDesktopJavaTime.java
Absolute Path2: C:UsersDesktop
Relative Path: /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

 


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\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:UsersDesktopJavaTime.java
Absolute Path2: C:UsersDesktop
Absolute Path: JavaTime.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.


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\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: JavaTime.java

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

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.