Java Path Relative getRelativeFile(File targetFile, File baseFile)

Here you can find the source of getRelativeFile(File targetFile, File baseFile)

Description

get Relative File

License

LGPL

Declaration

public static File getRelativeFile(File targetFile, File baseFile) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.io.*;

import java.util.regex.*;

public class Main {
    public static File getRelativeFile(File targetFile, File baseFile) {
        try {// w  w w.  j a v a2s  .c o m
            targetFile = targetFile.getCanonicalFile();
            baseFile = baseFile.getCanonicalFile();
        } catch (IOException ignored) {
        }
        String pathSeparator = File.separator;
        String basePath = baseFile.getAbsolutePath();
        String normalizedTargetPath = normalize(targetFile.getAbsolutePath(), pathSeparator);
        String normalizedBasePath = normalize(basePath, pathSeparator);

        String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator));
        String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator));

        StringBuilder common = new StringBuilder();

        int commonIndex = 0;
        while (commonIndex < target.length && commonIndex < base.length
                && target[commonIndex].equals(base[commonIndex])) {
            common.append(target[commonIndex]).append(pathSeparator);
            commonIndex++;
        }

        if (commonIndex == 0) {
            throw new Error("No common path element found for '" + normalizedTargetPath + "' and '"
                    + normalizedBasePath + '\'');
        }

        boolean baseIsFile = true;

        if (baseFile.exists()) {
            baseIsFile = baseFile.isFile();
        } else if (basePath.endsWith(pathSeparator)) {
            baseIsFile = false;
        }

        StringBuilder relative = new StringBuilder();

        if (base.length != commonIndex) {
            int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex;

            for (int i = 0; i < numDirsUp; i++) {
                relative.append("").append(pathSeparator);
            }
        }
        relative.append(normalizedTargetPath.substring(common.length()));
        return new File(relative.toString());
    }

    private static String normalize(String path, String separator) {
        path = path.replace(separator + separator, separator);
        path = path.replace(separator + separator, separator);
        if (path.endsWith(separator)) {
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }
}

Related

  1. getRelativeFile(File parent, final String s2)
  2. getRelativeFile(File source, String path)
  3. getRelativeFile(File subj, File relativeTo)
  4. getRelativeFile(File target, File base)
  5. getRelativeFile(File target, File baseDirectory)
  6. getRelativeFile(java.io.File srcFile, int upCount, String... childPaths)
  7. getRelativeFile(String baseDir, String fileName)
  8. getRelativeFileFromReference(String ref, File metadataFile)
  9. getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize)