Here you can find the source of getRelativePath(String parent, String child)
public static String getRelativePath(String parent, String child)
//package com.java2s; /*//from w ww . j a v a 2s. co m * ==================================================================== * Copyright (c) 2004-2012 TMate Software Ltd. All rights reserved. * * This software is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms * are also available at http://svnkit.com/license.html * If newer versions of this license are posted there, you may use a * newer version instead, at your option. * ==================================================================== */ import java.io.File; public class Main { public static String getRelativePath(String parent, String child) { parent = parent.replace(File.separatorChar, '/'); child = child.replace(File.separatorChar, '/'); String relativePath = getPathAsChild(parent, child); return relativePath == null ? "" : relativePath; } /** * Former pathIsChild. * * @param path * @param pathChild * @return */ public static String getPathAsChild(String path, String pathChild) { if (path == null || pathChild == null) { return null; } if (pathChild.compareTo(path) == 0) { return null; } if (path.length() == 0 && !pathChild.startsWith("/")) { return pathChild; } if (!path.endsWith("/")) { // We don't want to have /foobar being a child of /foo path = path + "/"; } if (pathChild.startsWith(path)) { return pathChild.substring(path.length()); } return null; } }