问题
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!
回答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.
回答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.
来源:https://stackoverflow.com/questions/14898224/get-relative-path-of-a-file-path