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

前端 未结 22 2310
小蘑菇
小蘑菇 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 11:05

    When using java.net.URI.relativize you should be aware of Java bug: JDK-6226081 (URI should be able to relativize paths with partial roots)

    At the moment, the relativize() method of URI will only relativize URIs when one is a prefix of the other.

    Which essentially means java.net.URI.relativize will not create ".."'s for you.

提交回复
热议问题