Java Path Relative getRelativeFile(File subj, File relativeTo)

Here you can find the source of getRelativeFile(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 File getRelativeFile(File subj, File relativeTo) 

Method Source Code


//package com.java2s;
/*//from w ww .j  a v a  2s. c om
 * 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 File getRelativeFile(File subj, File relativeTo) {
        return new File(getRelativePath(subj, relativeTo));
    }

    /**
     * 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. getRelativeBaseDirectory(final File processedFile)
  2. getRelativeChildDirectory(String parent, String child)
  3. getRelativeDirectoryPath(String in_filePath, String in_basePath)
  4. getRelativeFile(File parent, final String s2)
  5. getRelativeFile(File source, String path)
  6. getRelativeFile(File target, File base)
  7. getRelativeFile(File target, File baseDirectory)
  8. getRelativeFile(File targetFile, File baseFile)
  9. getRelativeFile(java.io.File srcFile, int upCount, String... childPaths)