niok-android / ch.tutteli.niok / java.nio.file.Path / relativeTo

relativeTo

fun Path.relativeTo(other: Path): Path (source)

Delegates to kotlin.io.relativeToOrNull.

Notice, that this function is not equivalent to Path.relativize. This function calculates the relative path from this path to other where Path.relativize calculates the relative path from other to this path. However, other.relativeTo(this) is not necessarily the same as this.relativize(other). There are small subtleties when it comes to ../ and ./ in one of the paths and depending on the JVM in use. Following an example using Oracle's JDK8

val a = Paths.get("a")
val b = Paths.get("./b")
a.relativeTo(b) // ../a
b.relativize(a) // .././a

Notice further, that Path.relativize seems to contain a bug (in Oracle's JDK8, seems to be fixed in JDK 9) when this path contains ./. Following an example

Paths.get("./a").relativize(Paths.get("b")) // results in ../../b instead of ../b (or .././b)

See https://bugs.java.com/bugdatabase/view_bug.do?bug_id=9057443 for further information. Hence, in case you want to use the result in Path.resolve later on, then you should prefer Path.relativeTo over Path.relativize.