Java Path Relative Get getRelativePath(File home, File f)

Here you can find the source of getRelativePath(File home, File f)

Description

get relative path of File 'f' with respect to 'home' directory example : home = /a/b/c f = /a/d/e/x.txt s = getRelativePath(home,f) = ../../d/e/x.txt home = "/a/b/c" file = "/a/b/c/d/e.txt" relative path = "d/e.txt" home = "/a/b/c" file = "/a/d/f/g.txt" relative path = "../../d/f/g.txt"

License

BSD License

Parameter

Parameter Description
home base path, should be a directory, not a file, or it doesn't make sense
f file to generate path for

Return

path from home to f as a string

Declaration

public static String getRelativePath(File home, File f) 

Method Source Code

//package com.java2s;
/**//from  ww  w . j a v  a  2  s . c  om
 * <p>
 * Utilities for manipulating Paths, Files, Directories, etc.
 * </p>
 * <p>
 * <span class="BSDLicense"> This software is distributed under the <a
 * href="http://hci.stanford.edu/research/copyright.txt">BSD License</a>.</span>
 * </p>
 * 
 * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu)
 */

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * get relative path of File 'f' with respect to 'home' directory example : home = /a/b/c f = /a/d/e/x.txt
     * s = getRelativePath(home,f) = ../../d/e/x.txt
     * 
     * home = "/a/b/c" file = "/a/b/c/d/e.txt" relative path = "d/e.txt"
     * 
     * home = "/a/b/c" file = "/a/d/f/g.txt" relative path = "../../d/f/g.txt"
     * 
     * @author David M. Howard
     * @param home
     *            base path, should be a directory, not a file, or it doesn't make sense
     * @param f
     *            file to generate path for
     * @return path from home to f as a string
     */
    public static String getRelativePath(File home, File f) {
        List<String> homelist;
        List<String> filelist;
        String s;

        homelist = getPathList(home);
        filelist = getPathList(f);
        s = matchPathLists(homelist, filelist);

        return s;
    }

    /**
     * break a path down into individual elements and add to a list. example : if a path is /a/b/c/d.txt, the
     * breakdown will be [d.txt,c,b,a]
     * 
     * @author David M. Howard
     * @param f
     *            input file
     * @return a List collection with the individual elements of the path in reverse order
     */
    private static List<String> getPathList(File f) {
        List<String> l = new ArrayList<String>();
        File r;
        try {
            r = f.getCanonicalFile();
            while (r != null) {
                l.add(r.getName());
                r = r.getParentFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
            l = null;
        }
        return l;
    }

    /**
     * figure out a string representing the relative path of 'f' with respect to 'r'
     * 
     * @author David M. Howard
     * 
     * @param r
     *            home path
     * @param f
     *            path of file
     */
    private static String matchPathLists(List<String> r, List<String> f) {
        int i;
        int j;
        String s;
        // start at the beginning of the lists
        // iterate while both lists are equal
        s = "";
        i = r.size() - 1;
        j = f.size() - 1;

        // first eliminate common root
        while ((i >= 0) && (j >= 0) && (r.get(i).equals(f.get(j)))) {
            i--;
            j--;
        }

        // for each remaining level in the home path, add a ..
        for (; i >= 0; i--) {
            s += ".." + File.separator;
        }

        // for each level in the file path, add the path
        for (; j >= 1; j--) {
            s += f.get(j) + File.separator;
        }

        // file name
        s += f.get(j);
        return s;
    }
}

Related

  1. getRelativePath(File fromFile, File toFile)
  2. getRelativePath(File fromFile, File toFile)
  3. getRelativePath(File fromFile, File toFile)
  4. getRelativePath(File fromFile, File toFile)
  5. getRelativePath(File fromFolder, File toFile)
  6. getRelativePath(File home, File f)
  7. getRelativePath(File home, File f)
  8. getRelativePath(File original, File directory)
  9. getRelativePath(File parent, File child)