Java Path Relative Get getRelativePath(File base, File absoluteFile)

Here you can find the source of getRelativePath(File base, File absoluteFile)

Description

get Relative Path

License

Open Source License

Declaration

public static File getRelativePath(File base, File absoluteFile) 

Method Source Code


//package com.java2s;
/*/*w w  w.j  a v a2s .c  o  m*/
 * Copyright ? 2013 oleg.cherednik (http://code.google.com/u/oleg.cherednik/)
 *
 * The copyright of the computer program is the property of oleg.cherednik. The program may
 * be used and/or copied in accordance with the terms and conditions of GNU Leser General Public License.
 */

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static File getRelativePath(File base, File absoluteFile) {
        checkAbsolutePath(base);
        checkAbsolutePath(absoluteFile);

        try {
            File res = base.toPath().relativize(absoluteFile.toPath()).toFile();
            return new File('.' + File.separator + res.getPath());
        } catch (Exception ignored) {
        }

        String[] basePath = getPathParts(base);
        String[] filePath = getPathParts(absoluteFile);

        if (!basePath[0].equals(filePath[0]))
            return absoluteFile;

        int i = 0;
        int j = 0;
        final int maxBasePath = basePath.length;
        final int maxFilePath = filePath.length;

        StringBuilder buf = new StringBuilder(".");

        for (int max = Math.min(maxBasePath, maxFilePath); i < max && j < max; i++, j++)
            if (!basePath[i].equals(filePath[j]))
                break;

        for (; i < maxBasePath; i++)
            buf.append(File.separator).append("..");
        for (; j < maxFilePath; j++)
            buf.append(File.separator).append(filePath[j]);

        return new File(buf.toString());
    }

    private static void checkAbsolutePath(File file) {
        if (file == null)
            throw new NullPointerException("File is not set");
        if (file.getPath().startsWith("."))
            throw new IllegalArgumentException("File is not absolute: " + file);
    }

    private static String[] getPathParts(File file) {
        assert file != null;

        File parent;
        List<String> path = new ArrayList<String>();
        String str = file.getPath();

        String[] arr = str.split("[\\\\|/]");

        return arr;

        //      do {
        //         if ((parent = file.getParentFile()) != null) {
        //            path.add(file.getName());
        //            file = parent;
        //         } else
        //            path.add(file.getPath());
        //      } while (parent != null);
        //
        //      Collections.reverse(path);
        //
        //      return path.toArray(new String[path.size()]);
    }
}

Related

  1. getRelativePath(@Nullable File projectFile, @Nullable File rootFile)
  2. getRelativePath(File a, File b, String pathSep)
  3. getRelativePath(File ancestor, File file)
  4. getRelativePath(File base, File file)
  5. getRelativePath(File base, File file)
  6. getRelativePath(File base, File file)
  7. getRelativePath(File base, File file)