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

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

Description

Determines the root relative path of file in a platform independent manner.

License

Open Source License

Parameter

Parameter Description
root the root directory or file
file the root contained file or directory

Return

the platform independent, relative path or an absolute path

Declaration

public static String getRelativePath(File root, File file) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011-2014 Torkild U. Resheim.
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  w  w. ja  v  a2 s  .c  o  m*/
 *   Torkild U. Resheim - initial API and implementation
 *******************************************************************************/

import java.io.File;

import java.util.ArrayList;

public class Main {
    /**
     * Determines the <i>root</i> relative path of <i>file</i> in a platform independent manner. The returned string is
     * a path starting from but excluding <i>root</i> using the '/' character as a directory separator. If the
     * <i>file</i> argument is a folder a trailing directory separator is added. if the <i>root</i> argument is a file,
     * it's parent folder will be used.
     * <p>
     * Note that if <i>file</i> is <b>not relative</b> to root, it's absolute path will be returned.
     * </p>
     *
     * @param root
     *            the root directory or file
     * @param file
     *            the root contained file or directory
     * @return the platform independent, relative path or an absolute path
     */
    public static String getRelativePath(File root, File file) {
        ArrayList<String> segments = new ArrayList<String>();
        if (root.isFile()) {
            root = root.getParentFile();
        }
        getPathSegments(root, file, segments);
        StringBuilder path = new StringBuilder();
        for (int p = 0; p < segments.size(); p++) {
            if (p > 0) {
                path.append('/');
            }
            path.append(segments.get(p));
        }
        if (file.isDirectory()) {
            path.append('/');
        }
        return path.toString();
    }

    /**
     * Creates a path segment list.
     *
     * @param root
     *            the root folder
     * @param file
     *            the destination file
     * @param segments
     * @see #getRelativePath(File, File)
     */
    private static void getPathSegments(File root, File file,
            ArrayList<String> segments) {
        if (root.equals(file) || file == null) {
            return;
        }
        segments.add(0, file.getName());
        getPathSegments(root, file.getParentFile(), segments);
    }
}

Related

  1. getRelativePath(File parentDirectory, File file)
  2. getRelativePath(File path, File base)
  3. getRelativePath(File path, File basePath)
  4. getRelativePath(File ref_file, File tst_file)
  5. getRelativePath(File root, File file)
  6. getRelativePath(File root, File target, String fileSeparator)
  7. getRelativePath(File rootDirectory, File file)
  8. getRelativePath(File source, File destination)
  9. getRelativePath(File source, File target)