Java Path Relative getRelativeLink(File target, File base)

Here you can find the source of getRelativeLink(File target, File base)

Description

Returns the path of one File relative to another.

License

Open Source License

Parameter

Parameter Description
target the target directory
base the base directory

Return

target's path relative to the base directory

Declaration

public static String getRelativeLink(File target, File base) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.util.regex.Pattern;

public class Main {
    public static final String SEPARATOR = "/";

    /**/*w ww. j  av a2  s .  co m*/
     * Returns the path of one File relative to another.
     * Returns the absolute path of the target file when there is no base file.
     * <p/>
     *
     * @param target the target directory
     * @param base   the base directory
     * @return target's path relative to the base directory
     */
    public static String getRelativeLink(File target, File base) {
        if (base == null) {
            return getFullPath(target);
        } else {
            return getRelativePath(target, base);
        }
    }

    public static String getFullPath(File f) {
        try {
            return f.getCanonicalPath().replace('\\', '/');
        } catch (IOException e) {
            throw new RuntimeException("Could not get canonical path of file " + f, e);
        }
    }

    /**
     * Returns the path of one File relative to another.
     * <p/>
     * From http://stackoverflow.com/questions/204784
     *
     * @param target the target directory
     * @param base   the base directory
     * @return target's path relative to the base directory
     */
    public static String getRelativePath(File target, File base) {
        String[] baseComponents;
        String[] targetComponents;
        baseComponents = getFullPath(base).split(Pattern.quote(SEPARATOR));
        targetComponents = getFullPath(target).split(Pattern.quote(SEPARATOR));

        // skip common components
        int index = 0;
        for (; index < targetComponents.length && index < baseComponents.length; ++index) {
            if (!targetComponents[index].equals(baseComponents[index]))
                break;
        }

        StringBuilder result = new StringBuilder();
        if (index != baseComponents.length) {
            // backtrack to base directory
            for (int i = index; i < baseComponents.length; ++i)
                result.append("..").append(SEPARATOR);
        }
        for (; index < targetComponents.length; ++index)
            result.append(targetComponents[index]).append(SEPARATOR);
        if (!target.getPath().endsWith("/") && !target.getPath().endsWith("\\")) {
            // remove final path separator
            result.delete(result.length() - SEPARATOR.length(), result.length());
        }
        return result.toString();
    }
}

Related

  1. getRelativeFileName(File target, File realativeTo)
  2. getRelativeFileName(String filePath, String directoryPath)
  3. getRelativeFilePath(File absolutePath, File reference)
  4. getRelativeFilePath(String filePath, String relativePathPrefix)
  5. getRelativeLastModifiedTimeFile()
  6. getRelativeName(File file, File root)
  7. getRelativeName(final File directory, final File file)
  8. getRelativeName(final File file)
  9. getRelativeParentDirectory(File base, File file)