Exercise:
Write a Java Program to get the relative path from two absolute paths.
1.Get using URI class
Click Here to View the Solution!
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\\CodeOfCode\\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();
}
}
}
Click Here to View the Output!
Absolute Path1: C:\Users\Desktop\CodeOfCode\Java\Time.java Absolute Path2: C:\Users\Desktop Relative Path: CodeOfCode/Java/Time.java
Click Here to View the Explanation!
- This program is used to find the relative path among the two absolute paths through the
URI class
. - In a
try catch block
, two absolute paths of a file are stored in theabsolutePath1
andabsolutuePath2
variables of theFile
type and printed. - Each absolute path is converted to a
URI
using the.toURI()
method of theJava.net.URI
class. - Both the paths are then compared through the
relativize()
method to withdraw the most relative path. - The obtained relative path is then converted to a string using the
.getPath()
method and store in a string variable path. - Finally, the relative path in path will be printed.
- The
catch block
will only deal with any occurringexception e
.
2.Get using String methods
Click Here to View the Solution!
import java.io.File;
class Main {
public static void main(String[] args) {
// Create file objects
File file1 = new File("C:\\Users\\Desktop\\CodeOfCode\\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);
}
}
Click Here to View the Output!
Absolute Path1: C:\Users\Desktop\CodeOfCode\Java\Time.java Absolute Path2: C:\Users\Desktop Absolute Path: \CodeOfCode\Java\Time.java
Click Here to View the Explanation!
- This program is used to find the relative path among the two absolute paths using
String methods
. - Initially, two
File
objectsfile1
andfile2
are created that will take the two given absolute paths as parameters. - Each path is converted to a string using the
.toString()
method and store in the string variablesabsolutePath1
andabsolutePath2
. - A
substring()
method is used to remove the path length fromabsolutePath1
which is equal to the path length ofabsolutePath2
and hence, store the remaining string of theabsolutePath1
inrelativePath
. It is expressed as follows:absolutePath1.substring(ansolutePath2.length())
- Finally, the absolute path stored in
relativePath
will be printed.
3. Get using java.nio.file package
Click Here to View the Solution!
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\\CodeOfCode\\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);
}
}
Click Here to View the Output!
Relative Path: CodeOfCode\Java\Time.java
Click Here to View the Explanation!
- This program is used to find the relative path among the two given absolute paths by using the package
java.nio.file
. - Initially, two
File
objects absolutePath1 andabsolutePath2
are created of the type Path that will take the absolute paths as parameters. - A relative path is extracted from both the absolute paths using the
relativize()
method of the interface Path and store in the variablerelativePath
using the following expression:absolutePath2.relativize(absolutePath1)
.
- Finally, the final path stored in the
relativePath
is printed.