Java Path Relative Get getRelativePath(String target, String base)

Here you can find the source of getRelativePath(String target, String base)

Description

Returns the path of one file relative to another.

License

LGPL

Parameter

Parameter Description
target The target path
base The base path

Return

The target's path relative to the base path

Declaration

public static String getRelativePath(String target, String base) 

Method Source Code

//package com.java2s;
/**/*  ww w . jav a  2  s . com*/
 * Copyright 2009-2012 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.io.File;

import java.util.regex.Pattern;

public class Main {
    /**
     * Returns the path of one file relative to another.
     * 
     * @param target
     *        The target path
     * @param base
     *        The base path
     * @return The target's path relative to the base path
     */
    public static String getRelativePath(String target, String base) {
        // See:
        // http://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls

        String split = Pattern.quote(File.separator);
        String[] baseSegments = base.split(split);
        String[] targetSegments = target.split(split);
        StringBuilder result = new StringBuilder();

        // Skip common segments
        int index = 0;
        for (; index < targetSegments.length && index < baseSegments.length; ++index) {
            if (!targetSegments[index].equals(baseSegments[index]))
                break;
        }

        // Backtrack to base directory
        int length = baseSegments.length;
        if (index != length) {
            for (int i = index; i < length; ++i) {
                // "." segments have no effect
                if (!baseSegments[i].equals("."))
                    result.append(".." + File.separator);
            }
        }

        for (; index < targetSegments.length; ++index)
            result.append(targetSegments[index] + File.separator);

        // Remove final path separator
        if (!target.endsWith(File.separator))
            result.delete(result.length() - 1, result.length());

        return result.toString();
    }
}

Related

  1. getRelativePath(String parent, String child)
  2. getRelativePath(String path, String base)
  3. getRelativePath(String root, String path)
  4. getRelativePath(String rootpath, String path)
  5. getRelativePath(String source, String target)
  6. getRelativePath(String targetPath, String basePath, String pathSeparator)
  7. getRelativePath(String targetPath, String basePath, String pathSeparator)
  8. getRelativePathOfZipEntry(final File fileToZip, final File base)
  9. getRelativePathRecursive(File file, File baseDir, String prefix)