Java Utililty Methods Relative Path Get

List of utility methods to do Relative Path Get

Description

The list of methods to do Relative Path Get are organized into topic(s).

Method

StringrelativePath(File home, File f)
get relative path of File 'f' with respect to 'home' directory example : home = /a/b/c f = /a/d/e/x.txt s = getRelativePath(home,f) = ../../d/e/x.txt
List<String> homelist = pathList(home);
List<String> filelist = pathList(f);
return matchPathLists(homelist, filelist).replace('\\', '/');
StringrelativePath(File IncludedFile, File UpperDirectory)
{ method
if (!IncludedFile.exists())
    throw new IllegalArgumentException(
            "requested file '" + IncludedFile.getAbsolutePath() + "' does not exist");
if (!UpperDirectory.exists())
    throw new IllegalArgumentException(
            "requested file '" + UpperDirectory.getAbsolutePath() + "' does not exist");
if (!UpperDirectory.isDirectory())
    throw new IllegalArgumentException(
...
StringrelativePath(File parent, File child)
get relative path of child to parent.
To be noticed, relative returned NOT start with "/" and file separator will be replaced by "/"
String parentPath = parent.getCanonicalPath();
String childPath = child.getCanonicalPath();
if (parentPath.equalsIgnoreCase(childPath)) {
    return "";
} else {
    return childPath.substring(parentPath.length() + 1).replace(File.separator, "/");
StringrelativePath(File parent, File child)
relative Path
Preconditions.checkArgument(contains(parent, child));
String parentName = conventPath(parent);
String childName = conventPath(child);
String relative = childName.substring(parentName.length());
if (relative.length() == 0)
    return "/";
return "/" + relative.substring(0, relative.length() - 1);
FilerelativePath(File path, File relativeTo)
Calculates the relative path of one path relative to another
File tmpPath = path;
File tmpRelativeTo = relativeTo;
if (tmpRelativeTo != null) {
    String t1 = getFirstPart(tmpPath);
    String t2 = getFirstPart(tmpRelativeTo);
    while (t2.equals(t1)) {
        tmpPath = getTail(tmpPath);
        tmpRelativeTo = getTail(tmpRelativeTo);
...
StringrelativePath(File root, File node)
Computes the path name of a file node relative to a given root node.
String rootPath = root.getCanonicalPath();
String nodePath = node.getCanonicalPath();
return nodePath.substring(rootPath.length() + 1);
StringrelativePath(File src, File dest)
relative Path
try {
    String[] srcSplit = (src.isDirectory() ? src : src.getParentFile()).getCanonicalPath()
            .split(Pattern.quote(File.separator));
    String[] destSplit = dest.getCanonicalPath().split(Pattern.quote(File.separator));
    StringBuilder sb = new StringBuilder();
    int i = 0;
    for (; i < destSplit.length && i < srcSplit.length; ++i) {
        if (!destSplit[i].equals(srcSplit[i]))
...
StringrelativePath(final File root, final File file)
artifact path from root/file relation
return root.toURI().relativize(file.toURI()).getPath();
StringrelativePath(final String inputPath, final File file)
relative Path
String filePath = normalizePath(file.getAbsolutePath());
String relativePath = filePath;
if (filePath.contains("/")) {
    relativePath = filePath.substring(0, filePath.lastIndexOf("/"));
relativePath = relativePath.substring(normalizePath(inputPath).length());
return relativePath;
StringrelativePath(String origin, String target)
relative Path
try {
    origin = (new File(origin)).getCanonicalPath();
    File targetFile = new File(target);
    if (targetFile.isAbsolute())
        target = targetFile.getCanonicalPath();
    else
        target = (new File(origin, target)).getCanonicalPath();
} catch (IOException e) {
...