Notice the expression, absolutePath1.substring(absolutePath2.length()) In the above example, we have converted the file paths to strings. String relativePath = absolutePath1.substring(absolutePath2.length()) getPath() - converts the Uri into a stringĮxample 2: Java program to get a relative path from two absolute path using String methods import java.io.File įile file1 = new File('C:\\Users\\Desktop\\Programiz\\Java\\Time.java') įile file2 = new File('C:\\Users\\Desktop').relativize() - extracts the relative path by comparing two absolute paths with one another.toURI() - converts the File object to a Uri.We have used the URI class to convert the absolute paths into the relative path. In the above example, we have two absolute paths named absolutePath1 and absolutePath2. Output Absolute Path1: C:\Users\Desktop\Programiz\Java\Time.java URI relativePath = path2.relativize(path1) create a relative path from the two paths
('Absolute Path1: ' + absolutePath1) įile absolutePath2 = new File('C:\\Users\\Desktop') Example 1: Java program to get a relative path from two absolute paths using URI class import java.io.File įile absolutePath1 = new File('C:\\Users\\Desktop\\Programiz\\Java\\Time.java')