Java Path Relative Get getRelativePath(String parent, String child)

Here you can find the source of getRelativePath(String parent, String child)

Description

get Relative Path

License

Open Source License

Declaration

public static String getRelativePath(String parent, String child) 

Method Source Code


//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;
    }
}

Related

  1. getRelativePath(String basePath, String path)
  2. getRelativePath(String basePath, String pathToRelativize)
  3. getRelativePath(String fileName, String projFile)
  4. getRelativePath(String filePath, String basePath)
  5. getRelativePath(String from, String to)
  6. getRelativePath(String path, String base)
  7. getRelativePath(String root, String path)
  8. getRelativePath(String rootpath, String path)
  9. getRelativePath(String source, String target)