Java Path Relative Get getRelativePath(File ref_file, File tst_file)

Here you can find the source of getRelativePath(File ref_file, File tst_file)

Description

get Relative Path

License

Open Source License

Declaration

public static String getRelativePath(File ref_file, File tst_file) 

Method Source Code

//package com.java2s;
/*//from  w  w w .ja v a 2s .c o m
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.io.File;

import java.io.IOException;

import java.util.Vector;

public class Main {
    private static File[] fileSysRoots;
    private static boolean fileSysRootsGot = false;

    /**
     * Constructs 'relative' path for given two paths. For example, for
     * "/aaa/bbb/ccc/ddd" and "/aaa/xxx/yyy" relative path would be
     * "../../../xxx/yyy". NOTE: this code is probably system dependent
     *
     * @param ref_path reference path
     * @param tst_path path to construct relative form for
     * @return constructed relative path
     */
    public static String getRelativePath(String ref_path, String tst_path) {
        String[] ref_arr = split(ref_path, File.separatorChar);
        String[] tst_arr = split(tst_path, File.separatorChar);
        int last_match_ind = -1;
        String res = "";

        for (int i = 0; i < ref_arr.length && i < tst_arr.length; i++) {
            if (!ref_arr[i].equals(tst_arr[i])) {
                break;
            }
            last_match_ind = i;
        }
        for (int i = 0; i < ref_arr.length - last_match_ind - 1; i++) {
            res += ".." + File.separator;
        }
        for (int i = last_match_ind + 1; i < tst_arr.length; i++) {
            res += tst_arr[i] + File.separator;
        }
        if (res.equals("")) {
            res = "." + File.separator;
        }
        return res.substring(0, res.length() - 1);
    }

    public static String getRelativePath(File ref_file, File tst_file) {
        File[] roots = getFileSystemRoots();
        String ref_path = null;
        String tst_path = null;

        try {
            ref_path = ref_file.getCanonicalPath();
            tst_path = tst_file.getCanonicalPath();
        } catch (IOException e) {
            ref_path = ref_file.getAbsolutePath();
            tst_path = tst_file.getAbsolutePath();
        }
        if (roots != null) {
            for (int i = 0; i < roots.length; i++) {
                String root = roots[i].getAbsolutePath();
                if (ref_path.startsWith(root) != tst_path.startsWith(root)) {
                    return null;
                }
            }
        }
        return getRelativePath(ref_path, tst_path);
    }

    public static String[] split(String s, char delim) {
        String str = s;
        if (str == null) {
            return null;
        }
        Vector v = new Vector();
        for (int ind = str.indexOf(delim); ind >= 0; ind = str.indexOf(delim)) {
            if (ind > 0) {
                v.addElement(str.substring(0, ind));
            }
            str = str.substring(ind + 1);
        }
        if (str.length() > 0) {
            v.addElement(str);
        }
        String[] res = new String[v.size()];
        v.copyInto(res);
        return res;
    }

    private static File[] getFileSystemRoots() {
        if (!fileSysRootsGot) {
            fileSysRoots = File.listRoots();
            fileSysRootsGot = true;
        }
        return fileSysRoots;
    }
}

Related

  1. getRelativePath(File parent, File child)
  2. getRelativePath(File parent, File f)
  3. getRelativePath(File parentDirectory, File file)
  4. getRelativePath(File path, File base)
  5. getRelativePath(File path, File basePath)
  6. getRelativePath(File root, File file)
  7. getRelativePath(File root, File file)
  8. getRelativePath(File root, File target, String fileSeparator)
  9. getRelativePath(File rootDirectory, File file)