kiliner.blogg.se

Java get file path directory
Java get file path directory










java get file path directory

To learn more about how substring works, visit Java String substring().Įxample 3: Get a relative path from two absolute paths using package import That is, the string represented by absolutePath2 is removed from absolutePath1. Here, the substring() method returns the part of absolutePath1 starting from index equal to the length of absolutePath2.

java get file path directory

Notice the expression, absolutePath1.substring(absolutePath2.length())

java get file path directory

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: 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: 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")












    Java get file path directory