Java Path Relative Get getRelativePath(File fromFile, File toFile)

Here you can find the source of getRelativePath(File fromFile, File toFile)

Description

Calculates a relative path from one file to another.

License

Open Source License

Parameter

Parameter Description
fromFile Source file to calculate relative path from.
toFile Target file.

Return

Relative path from `fromFile` to `toFile`.

Declaration

public static String getRelativePath(File fromFile, File toFile) throws IOException 

Method Source Code


//package com.java2s;
/*/*from www .jav a2 s  .c  o m*/
 * Copyright (c) 2015 Nikolay Zapolnov (zapolnov@gmail.com).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    /**
     * Calculates a relative path from one file to another.
     * @param fromFile Source file to calculate relative path from.
     * @param toFile Target file.
     * @return Relative path from `fromFile` to `toFile`.
     */
    public static String getRelativePath(File fromFile, File toFile) throws IOException {
        // This method is based on code from FileUtils.java of Apache Ant 1.9.5
        // Original code was licensed under the Apache 2.0 license
        //
        // --------------------------------------------------------------------
        // Apache Ant
        // Copyright 1999-2015 The Apache Software Foundation
        //
        // This product includes software developed at
        // The Apache Software Foundation (http://www.apache.org/).
        // --------------------------------------------------------------------

        String fromPath = fromFile.getCanonicalPath();
        String toPath = toFile.getCanonicalPath();

        // build the path stack info to compare
        String[] fromPathStack = fromPath.replace(File.separator, "/").split("/");
        String[] toPathStack = toPath.replace(File.separator, "/").split("/");

        if (0 < toPathStack.length && 0 < fromPathStack.length) {
            if (!fromPathStack[0].equals(toPathStack[0])) {
                // not the same device (would be "" on Linux/Unix)
                return String.join("/", toPathStack);
            }
        } else {
            // no comparison possible
            return String.join("/", toPathStack);
        }

        int minLength = Math.min(fromPathStack.length, toPathStack.length);
        int same = 1; // Used outside the for loop

        // get index of parts which are equal
        while (same < minLength && fromPathStack[same].equals(toPathStack[same]))
            ++same;

        List<String> relativePathStack = new ArrayList<>();

        // if "from" part is longer, fill it up with ".."
        // to reach path which is equal to both paths
        for (int i = same; i < fromPathStack.length; i++)
            relativePathStack.add("..");

        // fill it up path with parts which were not equal
        relativePathStack.addAll(Arrays.asList(toPathStack).subList(same, toPathStack.length));

        return String.join("/", relativePathStack);
    }

    /**
     * Retrieves canonical path for the provided file.
     * @param file File.
     * @return Canonical path to the file.
     */
    public static String getCanonicalPath(File file) {
        try {
            return file.getCanonicalPath();
        } catch (Throwable ignored) {
            return file.getAbsolutePath();
        }
    }
}

Related

  1. getRelativePath(File fileOrFolder, File baseFolder)
  2. getRelativePath(File folderContainingFile, File fileInFolder)
  3. getRelativePath(File fromDir, File toFile)
  4. getRelativePath(File fromFile, File toFile)
  5. getRelativePath(File fromFile, File toFile)
  6. getRelativePath(File fromFile, File toFile)
  7. getRelativePath(File fromFolder, File toFile)
  8. getRelativePath(File home, File f)
  9. getRelativePath(File home, File f)