Here you can find the source of getRelativePath(File folderContainingFile, File fileInFolder)
public static String getRelativePath(File folderContainingFile, File fileInFolder) throws IOException
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.IOException; public class Main { public static String getRelativePath(File folderContainingFile, File fileInFolder) throws IOException { String filePath = fileInFolder.getCanonicalPath(); String folderPath = folderContainingFile.getCanonicalPath(); return new File(folderPath).toURI().relativize(new File(filePath).toURI()).getPath(); }// w w w . jav a 2 s . c om }