Java Path Relative Get getRelativePath(File a, File b, String pathSep)

Here you can find the source of getRelativePath(File a, File b, String pathSep)

Description

get Relative Path

License

Apache License

Declaration

public static String getRelativePath(File a, File b, String pathSep) 

Method Source Code

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

import java.io.File;

import java.util.StringTokenizer;

public class Main {
    public static String getRelativePath(String a, String b, String pathSep) {
        String[] pathA = getPaths(a);
        String[] pathB = getPaths(b);

        // get common root.
        int indexA = -1;
        int indexB = -1;

        for (int i = pathA.length; 0 < i; i--) {
            if (lastIndexOf(pathA[i - 1], pathB) != -1) {
                indexA = i - 1;// w  w  w  .  j a v a2 s .  c om
                indexB = lastIndexOf(pathA[i - 1], pathB);
                break;
            }
        }

        if (indexA == -1 || indexB == -1) {
            return null;
        }

        int pathADistance = pathA.length - (indexA + 1);
        int pathBDistance = pathB.length - (indexB + 1);

        String relativePath = "";
        for (int i = 0; i < pathADistance; i++) {
            relativePath += ".." + pathSep;
        }
        //        if (pathADistance == 0) {
        //            relativePath = "." + File.separator;
        //        }

        String[] pathBComponents = getPathComponents(b);

        String sep = "";
        for (int i = pathB.length - pathBDistance; i < pathB.length; i++) {
            relativePath += sep + pathBComponents[i];
            sep = pathSep;
        }
        return relativePath;
    }

    public static String getRelativePath(String a, String b) {
        return getRelativePath(a, b, File.separator);
    }

    public static String getRelativePath(File a, File b, String pathSep) {
        return getRelativePath(a.getAbsolutePath(), b.getAbsolutePath(), pathSep);
    }

    public static String getRelativePath(File a, File b) {
        return getRelativePath(a.getAbsolutePath(), b.getAbsolutePath(), File.separator);
    }

    private static String[] getPaths(String str) {
        StringTokenizer tokens = new StringTokenizer(str, "\\/", false);
        String[] result = new String[tokens.countTokens()];
        String currentPath = "";
        String pathSep = "";
        for (int i = 0; i < result.length; i++) {
            currentPath = currentPath + pathSep + tokens.nextToken();
            result[i] = currentPath;
            pathSep = File.separator;
        }
        return result;
    }

    private static int lastIndexOf(String str, String[] path) {
        for (int i = path.length; 0 < i; i--) {
            if (path[i - 1].compareTo(str) == 0) {
                return i - 1;
            }
        }
        return -1;
    }

    private static String[] getPathComponents(String str) {
        StringTokenizer tokens = new StringTokenizer(str, "\\/", false);
        String[] result = new String[tokens.countTokens()];
        for (int i = 0; i < result.length; i++) {
            result[i] = tokens.nextToken();
        }
        return result;
    }
}

Related

  1. getRelativePath(@Nullable File projectFile, @Nullable File rootFile)
  2. getRelativePath(File ancestor, File file)
  3. getRelativePath(File base, File absoluteFile)
  4. getRelativePath(File base, File file)
  5. getRelativePath(File base, File file)