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

Here you can find the source of relativize(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 
Author: David M.

License

Apache 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 relativize(File home, File f) throws IOException 

Method Source Code


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

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

public class Main {
    /**//ww w .j  a v a 2  s. co m
     * get relative path of File 'f' with respect to 'home' directory
     * <PRE>
     * example : home = /a/b/c
     *           f    = /a/d/e/x.txt
     *           s = getRelativePath(home,f) = ../../d/e/x.txt
     * </PRE>
     * 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 relativize(File home, File f) throws IOException {
        List<String> homelist = getPathList(home);
        List<String> filelist = getPathList(f);

        String 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
     */
    protected static List<String> getPathList(File f) throws IOException {
        List<String> l = new ArrayList<String>();
        File r = f.getAbsoluteFile();
        while (r != null) {
            l.add(r.getName());
            r = r.getParentFile();
        }
        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
     */
    protected 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. relativeTo(File peer, String filename)
  2. relativeToAbsoluteFilePath(String s)
  3. relativize(File base, File absolute)
  4. relativize(File base, File child)
  5. relativize(File file, File basedir)
  6. relativize(File path, File relative)
  7. relativize(final File root, final File target)
  8. relativize(String path)
  9. relativize(String path)