Here you can find the source of getRelativePath(final File base, final File name)
public static String getRelativePath(final File base, final File name) throws IOException
//package com.java2s; /*/*from w w w . j av a 2s. c o m*/ * Copyright (C) 2006-2015 Ronald Blankendaal * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import java.io.File; import java.io.IOException; public class Main { public static String getRelativePath(final File base, final File name) throws IOException { File parent = base.getParentFile(); if (parent == null) return name.getPath(); String bpath = base.getCanonicalPath(); String fpath = name.getCanonicalPath(); if (fpath.startsWith(bpath)) { return fpath.substring(bpath.length() + 1); } else { return ".." + File.separatorChar + getRelativePath(parent, name); } } }