Java Relative Path Get relativeFile(Iterable paths, String file)

Here you can find the source of relativeFile(Iterable paths, String file)

Description

Given a path to a file and a list of "search paths" returns the relative path of the file (relative to the one search path that matched)

License

Apache License

Parameter

Parameter Description
paths A list of folders
file A path to a file

Return

The relative file path or the original file path if no match was found

Declaration

public static String relativeFile(Iterable<? extends File> paths, String file) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;

public class Main {
    /**/* ww w .j a v a2s  .  c  om*/
     * Given a file and a possible parent folder returns
     * the file relative to the parent. If the file wasn't
     * relative to the parent it is returned unmodified.
     * @param root A possible parent file
     * @param file A file
     * @return A file relative to the parent
     */
    public static File relativeFile(File root, File file) {
        if (root != null && file != null) {
            String absRoot = absoluteFile(root).getPath();
            String absFile = absoluteFile(file).getPath();
            if (absFile.startsWith(absRoot)) {
                String path = absFile.substring(absRoot.length());
                if (path.startsWith(File.separator)) {
                    path = path.substring(1);
                }
                file = new File(path);
            }
        }
        return file;
    }

    /**
     * Turns the given path, if absolute, into a path relative to the
     * VM's current working directory and leaves it alone otherwise 
     * @param f The File to make relative
     * @return A relative File
     */
    public static File relativeFile(File f) {
        if (f.isAbsolute()) {
            f = relativeFile(new File("."), f);
        }
        return f;
    }

    /**
     * Given a path to a file and a list of "search paths"
     * returns the relative path of the file (relative to the one
     * search path that matched)
     * @param paths A list of folders
     * @param file A path to a file 
     * @return The relative file path or the original file path if no match was found
     */
    public static String relativeFile(Iterable<? extends File> paths, String file) {
        // find the matching path prefix
        File path = selectPath(paths, file);
        // and get the path of the file relative to the prefix
        File relFile = relativeFile(path, new File(file));
        return relFile.getPath();
    }

    /**
     * Turns the given file into the best absolute representation available 
     * @param file A file
     * @return A canonical or absolute file
     */
    public static File absoluteFile(File file) {
        if (file != null) {
            try {
                file = file.getCanonicalFile();
            } catch (IOException e) {
                file = file.getAbsoluteFile();
            }
        }
        return file;
    }

    /**
     * Given a path to a file and a list of "search paths" returns
     * the search path that matched the path of the file
     * @param paths A list of folders
     * @param file A path to a file 
     * @return The search path where the file was located or null
     */
    public static File selectPath(Iterable<? extends File> paths, String file) {
        // make sure file is absolute and normalized
        file = absoluteFile(new File(file)).getPath();
        // find the matching path prefix
        int srcDirLength = 0;
        File srcDirFile = null;
        for (File prefixFile : paths) {
            String absPrefix = absoluteFile(prefixFile).getPath() + File.separatorChar;
            if (file.startsWith(absPrefix) && absPrefix.length() > srcDirLength) {
                srcDirLength = absPrefix.length();
                srcDirFile = prefixFile;
            }
        }
        return srcDirFile;
    }
}

Related

  1. relativeFile(File file, String relativeTo)
  2. relativeFileName(final File baseDir, final File file)
  3. relativeFrom(File from, File to)
  4. relativePath(File baseDir, File child)
  5. relativePath(File baseDir, File storeFile)