Get relative path of a File path? [duplicate]

风格不统一 提交于 2020-01-11 06:42:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!