Here you can find the source of relativizePath(Path root, Path child)
public static String relativizePath(Path root, Path child)
//package com.java2s; //License from project: Apache License import java.nio.file.*; public class Main { public static String relativizePath(Path root, Path child) { String childPath = child.toAbsolutePath().toString(); String rootPath = root.toAbsolutePath().toString(); if (childPath.equals(rootPath)) { return ""; }//from ww w .j a va 2 s . c o m int indexOfRootInChild = childPath.indexOf(rootPath); if (indexOfRootInChild != 0) { throw new IllegalArgumentException( "Child path " + childPath + "is not beginning with root path " + rootPath); } String relativizedPath = childPath.substring(rootPath.length(), childPath.length()); while (relativizedPath.startsWith(root.getFileSystem().getSeparator())) { relativizedPath = relativizedPath.substring(1); } return relativizedPath; } }