Java Utililty Methods Path Relative

List of utility methods to do Path Relative

Description

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

Method

StringgetRelative(File base, File path)
Get a relative path to a base directory.
return base.toURI().relativize(path.toURI()).getPath();
StringgetRelative(File base, File path)
Get the relative path of a file using a base path to specify the absolute portion.
String absBase = base.getAbsolutePath();
String absPath = path.getAbsolutePath();
if (absPath.indexOf(absBase) != 0)
    return absPath;
return absPath.substring(absBase.length() + (absPath.equals(absBase) ? 0 : 1));
StringgetRelative(String basedir, String child)
Given a basedir and a child file, return the relative path to the child.
if (basedir.endsWith("/") || basedir.endsWith("\\")) {
    basedir = basedir.substring(0, basedir.length() - 1);
if (child.startsWith(basedir)) {
    return child.substring(basedir.length() + 1);
String absoluteBasedir = new File(basedir).getAbsolutePath();
if (child.startsWith(absoluteBasedir)) {
...
StringgetRelativeAncestor(File ancestor, File descendant)
Get a relative path pointing from a descendant to an ancestor.
List ancestors = getAncestors(descendant);
if (!ancestors.contains(ancestor)) {
    throw new IllegalArgumentException("The file \"" + descendant.getPath() + "\" "
            + "does not have ancestor \"" + ancestor.getPath() + "\".");
Iterator i = ancestors.iterator();
File f;
do {
...
StringgetRelativeBaseDirectory(final File processedFile)
Retrieves the relative base directory from the currently processed file
return processedFile.getParentFile().getParentFile().getAbsolutePath();
StringgetRelativeChildDirectory(String parent, String child)
Given 2 paths, make sure parent and child are on the same tree return the port of child that not in parent
try {
    String childPath = new File(child).getCanonicalFile().getPath().replace('\\', '/');
    String parentPath = new File(parent).getCanonicalFile().getPath().replace('\\', '/');
    if (!childPath.startsWith(parentPath)) {
        throw new IllegalStateException();
    String retDir = "." + childPath.substring(parentPath.length());
    return retDir;
...
StringgetRelativeDirectoryPath(String in_filePath, String in_basePath)
get Relative Directory Path
String relativePath = new File(in_basePath).toURI().relativize(new File(in_filePath).toURI()).getPath();
return standardiseDirectoryPath(relativePath);
FilegetRelativeFile(File parent, final String s2)
get Relative File
File sf2 = new File(s2);
if (sf2.isAbsolute()) {
    return sf2;
if (!parent.isDirectory()) {
    parent = parent.getParentFile();
File file = new File(parent, s2);
...
FilegetRelativeFile(File source, String path)
Construct the File that is separated from the given File by the given relative path.
source = source.getParentFile();
return new File(source, path);
FilegetRelativeFile(File subj, File relativeTo)
Gets the relative representations of a file compared to another.
return new File(getRelativePath(subj, relativeTo));