1

If I have a path /a/b/c/d/e and I want to remove some of the path from the root - for example remove /a/b/c from it to get just /d/e it is very simple in python:

os.path.relpath("/a/b/c/d/e", "/a/b/c")

The question is how to accomplish this in java. The best idea I have come up with so far is to create a Path for each, create an iterator, loop on it and create a Path array. Then check the size of each of the arrays (5 and 3 in the above example) and then rebuild the path from the 4th location ... very cumbersome..

hnt
  • 11
  • 2
  • https://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls – Chrispresso Feb 09 '20 at 20:01

1 Answers1

0

In Groovy:

def rootPath = new File('/a/b')
def fullPath = new File('/a/b/c/d')

def relPath = new File(root.toURI().relativize(full.toURI()).toString())

Source

Cloudkollektiv
  • 11,852
  • 3
  • 44
  • 71