Here you can find the source of relativizePath(Path relativeTo, String filename)
public static String relativizePath(Path relativeTo, String filename)
//package com.java2s; //License from project: Apache License import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static String relativizePath(Path relativeTo, String filename) { if (relativeTo == null) { return filename; }//from w w w .ja v a 2s . c o m Path path = getAbsolutePath(filename); if (path.startsWith(relativeTo)) { return relativeTo.relativize(path).toString(); } else { return path.toString(); } } public static Path getAbsolutePath(String name) { return Paths.get(name).toAbsolutePath(); } }