Java Path Relative Get getRelativePath(File base, File file)

Here you can find the source of getRelativePath(File base, File file)

Description

Finds the relative path from base to file.

License

Open Source License

Parameter

Parameter Description
base The base for the relative path
file The destination for the relative path

Return

The relative path betwixt base and file, or null if there was a IOException

Declaration

public static File getRelativePath(File base, File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 MadRobot.//  w ww . ja v  a 2  s. co  m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Finds the relative path from base to file. The file returned is such that file.getCanonicalPath().equals( new
     * File( base, getRelativePath( base, file ).getPath() ).getCanonicalPath )
     * 
     * @param base
     *            The base for the relative path
     * @param file
     *            The destination for the relative path
     * @return The relative path betwixt base and file, or null if there was a IOException
     */
    public static File getRelativePath(File base, File file) {
        try {
            String absBase = base.getCanonicalPath();
            String absFile = file.getCanonicalPath();

            if (absBase.equals(absFile)) { // base and file are the same file
                return new File("");
            }

            // find divergence point
            int divergenceCharIndex = 0;
            int divergenceSeperatorIndex = 0;

            while ((divergenceCharIndex < absBase.length()) && (divergenceCharIndex < absFile.length())
                    && (absBase.charAt(divergenceCharIndex) == absFile.charAt(divergenceCharIndex))) {
                if ((absBase.charAt(divergenceCharIndex) == File.separatorChar)
                        || (absFile.charAt(divergenceCharIndex) == File.separatorChar)) {
                    divergenceSeperatorIndex = divergenceCharIndex;
                }

                divergenceCharIndex++;
            }

            if (divergenceCharIndex == absBase.length()) { // simple case where
                // the base is an
                // ancestor of the
                // target
                // file
                return new File(absFile.substring(divergenceCharIndex + 1));
            } else { // we need to back up the tree a bit, before coming back
                // down.
                StringBuilder path = new StringBuilder();
                for (int i = divergenceSeperatorIndex; i < absBase.length(); i++) {
                    if (absBase.charAt(i) == File.separatorChar) {
                        path.append("..");
                        path.append(File.separatorChar);
                    }
                }

                path.append(absFile.substring(divergenceSeperatorIndex));

                return new File(path.toString());
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return null;
        }
    }
}

Related

  1. getRelativePath(@Nullable File projectFile, @Nullable File rootFile)
  2. getRelativePath(File a, File b, String pathSep)
  3. getRelativePath(File ancestor, File file)
  4. getRelativePath(File base, File absoluteFile)
  5. getRelativePath(File base, File file)
  6. getRelativePath(File base, File file)
  7. getRelativePath(File base, File file)
  8. getRelativePath(File base, File name)