Here you can find the source of getRelativePath(File baseFile, File file, String resultIfImpossible)
public static String getRelativePath(File baseFile, File file, String resultIfImpossible)
//package com.java2s; //License from project: Apache License import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static String getRelativePath(File baseFile, File file, String resultIfImpossible) { Path pathBase = Paths.get(baseFile.getAbsolutePath()); Path pathAbsolute = Paths.get(file.getAbsolutePath()); try {//from w w w . j a v a 2 s . c o m Path pathRelative = pathBase.relativize(pathAbsolute); return pathRelative.toString(); } catch (Exception e) { return resultIfImpossible; } } }