Java Utililty Methods Path Relative Get

List of utility methods to do Path Relative Get

Description

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

Method

StringgetRelativePath(File fromFile, File toFile)
Calculates a relative path from one file to another.
String fromPath = fromFile.getCanonicalPath();
String toPath = toFile.getCanonicalPath();
String[] fromPathStack = fromPath.replace(File.separator, "/").split("/");
String[] toPathStack = toPath.replace(File.separator, "/").split("/");
if (0 < toPathStack.length && 0 < fromPathStack.length) {
    if (!fromPathStack[0].equals(toPathStack[0])) {
        return String.join("/", toPathStack);
} else {
    return String.join("/", toPathStack);
int minLength = Math.min(fromPathStack.length, toPathStack.length);
int same = 1; 
while (same < minLength && fromPathStack[same].equals(toPathStack[same]))
    ++same;
List<String> relativePathStack = new ArrayList<>();
for (int i = same; i < fromPathStack.length; i++)
    relativePathStack.add("..");
relativePathStack.addAll(Arrays.asList(toPathStack).subList(same, toPathStack.length));
return String.join("/", relativePathStack);
StringgetRelativePath(File fromFile, File toFile)
get Relative Path
File fromDir = fromFile.isDirectory() ? fromFile : fromFile.getParentFile();
File toDir = toFile.isDirectory() ? toFile : toFile.getParentFile();
File dir = fromDir;
String relPath = "";
while (dir != null && !dir.equals(toDir)) {
    relPath += "../";
    dir = dir.getParentFile();
if (dir == null) {
    throw new RuntimeException(toFile + " is not in parent of " + fromFile);
return relPath + toFile.toString();
StringgetRelativePath(File fromFolder, File toFile)
Gets the relative path of a file wrt one of its parent directories.
String basePath = fromFolder.getAbsolutePath();
String filePath = toFile.getAbsolutePath();
if (!filePath.startsWith(basePath))
    throw new IOException("The specified file is not within the folder.");
return filePath.substring(basePath.length() + 1);
StringgetRelativePath(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 home = "/a/b/c" file = "/a/b/c/d/e.txt" relative path = "d/e.txt" home = "/a/b/c" file = "/a/d/f/g.txt" relative path = "../../d/f/g.txt"
List<String> homelist;
List<String> filelist;
String s;
homelist = getPathList(home);
filelist = getPathList(f);
s = matchPathLists(homelist, filelist);
return s;
StringgetRelativePath(File home, File f)
get Relative Path
List<String> homelist;
List<String> filelist;
String s;
homelist = getPathList(home);
filelist = getPathList(f);
s = matchPathLists(homelist, filelist);
return s;
StringgetRelativePath(File home, File f)
get Relative Path
return matchPathLists(getPathList(home), getPathList(f));
StringgetRelativePath(File original, File directory)
Return a relative path to original as seen from given directory.
String result = null;
try {
    String o = original.getCanonicalPath();
    String dir = directory.getCanonicalPath() + File.separator;
    int lastCommon = 0;
    int index = dir.indexOf(File.separator);
    while (index >= 0) {
        if (!dir.regionMatches(true, lastCommon, o, lastCommon, index - lastCommon))
...
FilegetRelativePath(File parent, File child)
Returns the relative path to a file with respect to a parent directory.
parent = parent.getCanonicalFile();
File file = child.getCanonicalFile();
File rval = new File(file.getName());
while ((file = file.getParentFile()) != null) {
    if (parent.equals(file)) {
        return rval;
    rval = new File(file.getName(), rval.getPath());
...
StringgetRelativePath(File parent, File child)
get Relative Path
if (parent.isFile() || !child.getPath().startsWith(parent.getPath())) {
    return null;
return child.getPath().substring(parent.getPath().length() + 1);
StringgetRelativePath(File parent, File f)
Creates a description of f relative to parent.
File dir;
if (parent.isDirectory()) {
    dir = parent;
} else {
    dir = parent.getParentFile();
String s = isAncestor(dir, f.getParentFile());
if (s != null) {
...