Here you can find the source of getRelativePath(File fromFile, File toFile)
public static String getRelativePath(File fromFile, File toFile)
//package com.java2s; import java.io.*; public class Main { public static String getRelativePath(File fromFile, File toFile) { File fromDir = fromFile.isDirectory() ? fromFile : fromFile.getParentFile(); File toDir = toFile.isDirectory() ? toFile : toFile.getParentFile(); File dir = fromDir;/* w w w . j a v a 2 s . c o m*/ String relPath = ""; while (dir != null && !dir.equals(toDir)) { relPath += "../"; dir = dir.getParentFile(); } if (dir == null) { throw new RuntimeException(toFile + " is not in parent of " + fromFile); } return relPath + toFile.toString(); } }