Here you can find the source of getRelativePathRecursive(File file, File baseDir, String prefix)
private static String getRelativePathRecursive(File file, File baseDir, String prefix)
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { private static String getRelativePathRecursive(File file, File baseDir, String prefix) { if (isFileInDirRecursive(file, baseDir)) { return prefix + baseDir.toURI().relativize(file.toURI()).getPath(); } else if (baseDir.getParentFile() != null) { return prefix + getRelativePathRecursive(file, baseDir.getParentFile(), "../"); } else {/*ww w .ja v a2 s . co m*/ return file.toURI().toString(); } } protected static boolean isFileInDirRecursive(File file, File dir) { if (file.getParentFile() != null) { return file.getParentFile().equals(dir) || isFileInDirRecursive(file.getParentFile(), dir); } return false; } }