Java Path Relative Get getRelativePath(File currentDir, File target)

Here you can find the source of getRelativePath(File currentDir, File target)

Description

Constructs a relative path that addresses a given file target from a current directory.

License

Apache License

Parameter

Parameter Description
currentDir Must be absolute
target Must be absolute

Return

Relative path from currentDir to target

Declaration

public static File getRelativePath(File currentDir, File target) 

Method Source Code

//package com.java2s;
/* GROOVE: GRaphs for Object Oriented VErification
 * Copyright 2003--2010 University of Twente
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * $Id: Util.java 5482 2014-07-20 22:25:43Z rensink $
 *///from w  ww  .jav a 2s  .c  om

import java.io.File;

public class Main {
    /**
     * Constructs a relative path that addresses a given file
     * target from a current directory.
     * @param currentDir Must be absolute
     * @param target Must be absolute
     * @return Relative path from currentDir to target
     */
    public static File getRelativePath(File currentDir, File target) {
        if (currentDir.isFile()) {
            currentDir = new File(currentDir.getParent());
        }
        if (!currentDir.isAbsolute() || !target.isAbsolute()) {
            return null;
        }

        String[] dirParts = currentDir.toString().split("\\Q" + File.pathSeparator + "\\E");
        String[] targetParts = target.toString().split("\\Q" + File.pathSeparator + "\\E");

        int i = 0;
        int max = Math.max(dirParts.length, targetParts.length);
        while (i < max && dirParts[i].equals(targetParts[i])) {
            i++;
        }
        StringBuilder relPath = new StringBuilder();
        int j = i;
        while (j < dirParts.length) {
            relPath.append(".." + File.pathSeparator);
            j++;
        }
        while (i < targetParts.length) {
            relPath.append(targetParts[i]);
            i++;
            if (i < targetParts.length) {
                relPath.append(File.pathSeparator);
            }
        }

        return new File(relPath.toString());
    }
}

Related

  1. getRelativePath(File baseDir, File file)
  2. getRelativePath(File baseDir, File subFile, String seperator)
  3. getRelativePath(File baseFile, File file)
  4. getRelativePath(File baseFolder, File subject)
  5. getRelativePath(File child, File directory)
  6. getRelativePath(File dir, File child)
  7. getRelativePath(File dir, File file)
  8. getRelativePath(File f, File base)
  9. getRelativePath(File file)