Here you can find the source of getRelativePath(File baseDir, File subFile, String seperator)
public static String getRelativePath(File baseDir, File subFile, String seperator)
//package com.java2s; //License from project: Apache License import java.io.File; public class Main { public static String getRelativePath(File baseDir, File subFile, String seperator) { String basePath = baseDir.getAbsolutePath(); String filePath = subFile.getAbsolutePath(); if (!filePath.startsWith(basePath)) { return null; }//from w ww . j a va2s . com String relativePath = filePath.substring(basePath.length() + 1); return seperator == null ? relativePath : relativePath.replaceAll("\\\\", seperator).replaceAll("/", seperator); } public static String getRelativePath(File baseDir, File file) { return getRelativePath(baseDir, file, null); } }