Java Path Relative Get getRelativePath(String from, String to)

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

Description

Returns the relative path from one file to the other.

License

Open Source License

Parameter

Parameter Description
from the path of the origin file
to the path of the destination file

Return

the relative path from origin to destination

Declaration

public static String getRelativePath(String from, String to) 

Method Source Code


//package com.java2s;
/*/*from ww w.j  a v a2 s  . c o  m*/
 *  Tiled Map Editor, (c) 2004-2006
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Adam Turk <aturk@biggeruniverse.com>
 *  Bjorn Lindeijer <bjorn@lindeijer.nl>
 */

import java.io.File;
import java.io.IOException;
import java.util.Vector;

public class Main {
    /**
     * Returns the relative path from one file to the other. The function
     * expects absolute paths, relative paths will be converted to absolute
     * using the working directory.
     *
     * @param from the path of the origin file
     * @param to   the path of the destination file
     * @return     the relative path from origin to destination
     */
    public static String getRelativePath(String from, String to) {
        if (!(new File(to)).isAbsolute())
            return to;

        File fromFile = new File(from);
        if (fromFile.exists() && !fromFile.isDirectory())
            fromFile = fromFile.getParentFile();
        // Make the two paths absolute and unique
        try {
            from = fromFile.getCanonicalPath();
            to = new File(to).getCanonicalPath();
        } catch (IOException e) {
        }

        File toFile = new File(to);
        Vector<String> fromParents = new Vector<String>();
        Vector<String> toParents = new Vector<String>();

        // Iterate to find both parent lists
        while (fromFile != null) {
            fromParents.add(0, fromFile.getName());
            fromFile = fromFile.getParentFile();
        }
        while (toFile != null) {
            toParents.add(0, toFile.getName());
            toFile = toFile.getParentFile();
        }

        // Iterate while parents are the same
        int shared = 0;
        int maxShared = Math.min(fromParents.size(), toParents.size());
        for (shared = 0; shared < maxShared; shared++) {
            String fromParent = fromParents.get(shared);
            String toParent = toParents.get(shared);
            if (!fromParent.equals(toParent)) {
                break;
            }
        }

        // Append .. for each remaining parent in fromParents
        StringBuffer relPathBuf = new StringBuffer();
        for (int i = shared; i < fromParents.size() - 1; i++) {
            relPathBuf.append(".." + File.separator);
        }

        // Add the remaining part in toParents
        for (int i = shared; i < toParents.size() - 1; i++) {
            relPathBuf.append(toParents.get(i) + File.separator);
        }
        relPathBuf.append(new File(to).getName());
        String relPath = relPathBuf.toString();

        // Turn around the slashes when path is relative
        try {
            String absPath = new File(relPath).getCanonicalPath();

            if (!absPath.equals(relPath)) {
                // Path is not absolute, turn slashes around
                // Assumes: \ does not occur in file names
                relPath = relPath.replace('\\', '/');
            }
        } catch (IOException e) {
        }

        return relPath;
    }
}

Related

  1. getRelativePath(String baseFullPath, File file)
  2. getRelativePath(String basePath, String path)
  3. getRelativePath(String basePath, String pathToRelativize)
  4. getRelativePath(String fileName, String projFile)
  5. getRelativePath(String filePath, String basePath)
  6. getRelativePath(String parent, String child)
  7. getRelativePath(String path, String base)
  8. getRelativePath(String root, String path)
  9. getRelativePath(String rootpath, String path)