Java Path Relative Get getRelativePathRecursive(File file, File baseDir, String prefix)

Here you can find the source of getRelativePathRecursive(File file, File baseDir, String prefix)

Description

get Relative Path Recursive

License

Apache License

Declaration

private static String getRelativePathRecursive(File file, File baseDir, String prefix) 

Method Source Code


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

import java.io.File;

public class Main {
    private static String getRelativePathRecursive(File file, File baseDir, String prefix) {
        if (isFileInDirRecursive(file, baseDir)) {
            return prefix + baseDir.toURI().relativize(file.toURI()).getPath();
        } else if (baseDir.getParentFile() != null) {
            return prefix + getRelativePathRecursive(file, baseDir.getParentFile(), "../");
        } else {/*ww w  .ja  v  a2  s  . co  m*/
            return file.toURI().toString();
        }

    }

    protected static boolean isFileInDirRecursive(File file, File dir) {
        if (file.getParentFile() != null) {
            return file.getParentFile().equals(dir) || isFileInDirRecursive(file.getParentFile(), dir);
        }
        return false;
    }
}

Related

  1. getRelativePath(String source, String target)
  2. getRelativePath(String target, String base)
  3. getRelativePath(String targetPath, String basePath, String pathSeparator)
  4. getRelativePath(String targetPath, String basePath, String pathSeparator)
  5. getRelativePathOfZipEntry(final File fileToZip, final File base)
  6. getRelativePathToBase(File path, File basePath)
  7. getRelativePathToTestModule()
  8. getRelativePathToWWW(String fileName)