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

前端 未结 22 2742
小蘑菇
小蘑菇 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:56

    If you're writing a Maven plugin, you can use Plexus' PathTool:

    import org.codehaus.plexus.util.PathTool;
    
    String relativeFilePath = PathTool.getRelativeFilePath(file1, file2);
    

提交回复
热议问题