获取文件路径的相对路径? [英] Get relative path of a File path?
问题描述
我需要从 File
对象获取相对文件路径(相对于程序可执行路径)。
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?
我在Java 7上,以防java.nio有一些帮助方法。
I am on Java 7, just in case java.nio has some helping method for that.
Tahnks任何提示!
Tahnks for any hint!
推荐答案
当你使用Java 7时,你可以使用新的Path类,它有一个数字非常酷的方法,包括 Path.relativize 。
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.
这篇关于获取文件路径的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!