Here you can find the source of getRelativePath(File file, File baseDirectory)
public static String getRelativePath(File file, File baseDirectory) throws Exception
//package com.java2s; import java.io.File; public class Main { /**//from w ww. j av a2s .c om * Given a file and base directory, the method returns file path relative to * base directory. If file is not under base directory, the method throws * IllegalArgumentException */ public static String getRelativePath(File file, File baseDirectory) throws Exception { String baseDirAsString = baseDirectory.getCanonicalPath(); String filePathAsString = file.getCanonicalPath(); int dirIndex = filePathAsString.indexOf(baseDirAsString); if (dirIndex == -1) { throw new IllegalArgumentException("Directory path is not part of file path. directory = " + baseDirAsString + " scenario path = " + filePathAsString); } return filePathAsString.substring(dirIndex + baseDirAsString.length() + 1); } }