How to construct a relative path in Java from two absolute paths (or URLs)?

前端 未结 22 2634
小蘑菇
小蘑菇 2020-11-22 10:30

Given two absolute paths, e.g.

/var/data/stuff/xyz.dat
/var/data

How can one create a relative path that uses the second path as its base?

22条回答
  •  失恋的感觉
    2020-11-22 10:51

    In Java 7 and later you can simply use (and in contrast to URI, it is bug free):

    Path#relativize(Path)
    

提交回复
热议问题