Get relative path of a File path?
12,633
Solution 1
As you're using Java 7 you can make use of the new Path class, which has a number of really cool methods, including Path.relativize.
Solution 2
You need the method:
Path relativize(Path other)
of java.nio.file.Path
.
To obtain a Path from a file you could use its Path toPath()
method.
Related videos on Youtube
Author by
stefan.at.wpf
Updated on June 04, 2022Comments
-
stefan.at.wpf almost 2 years
I need to get the relative file path (relative to the program executable path) from a
File
object.What's the best way to do this?
File
offers only methods for the absolute path. Maybe getting execution path manually and then cut this path off from the absolute path to get a relative path?I am on Java 7, just in case java.nio has some helping method for that.
Tahnks for any hint!
-
ycomp over 7 yearssuch a cool name for such a useful method