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? In the example above, the result should be: ./stuff/xyz.dat
filejavapathurl
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? In the example above, the result should be: ./stuff/xyz.dat
Best Answer
It's a little roundabout, but why not use URI? It has a relativize method which does all the necessary checks for you.
Please note that for file path there's
java.nio.file.Path#relativize
since Java 1.7, as pointed out by @Jirka Meluzin in the other answer.