Java Path Relative Get getRelativePath(final File from, final File to)

Here you can find the source of getRelativePath(final File from, final File to)

Description

get Relative Path

License

LGPL

Declaration

static String getRelativePath(final File from, final File to) 

Method Source Code


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

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

public class Main {
    private static final boolean LIKE_JAVA_NIO_PATH_RELATIVIZE = true;

    static String getRelativePath(final File from, final File to) {
        return getRelativePath(from, to, File.separatorChar);
    }/*from ww  w . ja v a2s .  c o  m*/

    static String getRelativePath(final File from, final File to, final char separatorChar) {
        final String fromPath = from.getAbsolutePath();
        final String toPath = to.getAbsolutePath();
        final boolean isDirectory = from.isDirectory();
        return getRelativePath(fromPath, toPath, isDirectory, separatorChar);
    }

    static String getRelativePath(final String fromPath, final String toPath, boolean isFromPathDirectory,
            final char separatorChar) {
        isFromPathDirectory = LIKE_JAVA_NIO_PATH_RELATIVIZE || isFromPathDirectory; //from observation, left for explanation
        final ArrayList<String> fromPathSegments = splitPath(fromPath);
        final ArrayList<String> toPathSegments = splitPath(toPath);
        while (!fromPathSegments.isEmpty() && !toPathSegments.isEmpty()) {
            if (!(fromPathSegments.get(0).equals(toPathSegments.get(0)))) {
                break;
            }
            fromPathSegments.remove(0);
            toPathSegments.remove(0);
        }

        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < fromPathSegments.size() - (isFromPathDirectory ? 0 : 1); i++) {
            sb.append("..");
            sb.append(separatorChar);
        }
        for (final String s : toPathSegments) {
            sb.append(s);
            sb.append(separatorChar);
        }
        return sb.substring(0, sb.length() - 1);
    }

    private static ArrayList<String> splitPath(final String path) {
        final ArrayList<String> pathSegments = new ArrayList<String>();
        final StringTokenizer st = new StringTokenizer(path, File.separator);
        while (st.hasMoreTokens()) {
            final String token = st.nextToken();
            if (token.equals(".")) {
                // skip
            } else if (token.equals("..")) {
                if (!pathSegments.isEmpty()) {
                    pathSegments.remove(pathSegments.size() - 1);
                }
            } else {
                pathSegments.add(token);
            }
        }
        return pathSegments;
    }
}

Related

  1. getRelativePath(final File base, final File child)
  2. getRelativePath(final File base, final File name)
  3. getRelativePath(final File baseDirectory, final File f)
  4. getRelativePath(final File basePathFile, final File pathFile)
  5. getRelativePath(final File file, final File folder)
  6. getRelativePath(final File fromFile, final File toFile)
  7. getRelativePath(final File parentDir, final File file)
  8. getRelativePath(final File parentDirectory, final File file)
  9. getRelativePath(final String base, final File targetFile)