Java Path Relative Get getRelativePath(File subj, File relativeTo)

Here you can find the source of getRelativePath(File subj, File relativeTo)

Description

Gets the relative representations of a file compared to another.

License

BSD License

Parameter

Parameter Description
subj The File to find the relative form for.
relativeTo The File 'subj' should be made relative to.

Return

The relative representation of subj.

Declaration

public static String getRelativePath(File subj, File relativeTo) 

Method Source Code


//package com.java2s;
/*/*from   w  ww.j  a  va2  s  . c o  m*/
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */

import java.io.File;

import java.util.StringTokenizer;

public class Main {
    /**
     * Gets the relative representations of a file compared to another.
     * 
     * @param subj
     *            The File to find the relative form for.
     * @param relativeTo
     *            The File 'subj' should be made relative to.
     * @return The relative representation of subj.
     */
    public static String getRelativePath(File subj, File relativeTo) {
        // Get the absolute path to both files.
        String subjPath = subj.getAbsolutePath();
        String relativeToPath = relativeTo.getAbsolutePath();

        // Remove the filenames
        if (!subj.isDirectory()) {
            int idx = subjPath.lastIndexOf(File.separator);
            if (idx != -1) {
                subjPath = subjPath.substring(0, idx);
            }
        }
        if (!relativeTo.isDirectory()) {
            int idx = relativeToPath.lastIndexOf(File.separator);
            if (idx != -1) {
                relativeToPath = relativeToPath.substring(0, idx);
            }
        }

        // Check all common directories, starting with the root.
        StringTokenizer subjPathTok = new StringTokenizer(subjPath, File.separator);
        StringTokenizer relativeToPathTok = new StringTokenizer(relativeToPath, File.separator);

        String subjTok = null;
        String relativeToTok = null;
        while (subjPathTok.hasMoreTokens() && relativeToPathTok.hasMoreTokens()) {
            subjTok = subjPathTok.nextToken();
            relativeToTok = relativeToPathTok.nextToken();
            if (!subjTok.equals(relativeToTok)) {
                break;
            }
        }

        // If there are any tokens left, than these should be made relative.
        // The number of tokens left in 'relativeToTok' is the number of '..'.
        StringBuilder relPath = new StringBuilder();

        if (!subjTok.equals(relativeToTok)) {
            // That's one dir
            relPath.append("..");
            relPath.append(File.separator);
        }
        while (relativeToPathTok.hasMoreTokens()) {
            relativeToPathTok.nextToken();
            relPath.append("..");
            relPath.append(File.separator);
        }

        // Now add the path to 'subj'
        if (!subjTok.equals(relativeToTok)) {
            relPath.append(subjTok);
            relPath.append(File.separator);
        }
        while (subjPathTok.hasMoreTokens()) {
            subjTok = subjPathTok.nextToken();
            relPath.append(subjTok);
            relPath.append(File.separator);
        }

        // Last but not least, add the filename of 'subj'
        relPath.append(subj.getName());

        return relPath.toString();
    }
}

Related

  1. getRelativePath(File root, File target, String fileSeparator)
  2. getRelativePath(File rootDirectory, File file)
  3. getRelativePath(File source, File destination)
  4. getRelativePath(File source, File target)
  5. getRelativePath(File src, File dst)
  6. getRelativePath(File target, File base)
  7. getRelativePath(File target, File relativeTo)
  8. getRelativePath(File wd, File file)
  9. getRelativePath(final File base, final File child)