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

FilegetRelativeFile(File target, File base)
Returns the path of one file relative to another.
return new File(getRelativePath(target.getAbsolutePath(), base.getAbsolutePath()));
FilegetRelativeFile(File target, File baseDirectory)
From http://stackoverflow.com /a/1269907.
String[] baseComponents = baseDirectory.getCanonicalPath().split(Pattern.quote(File.separator));
String[] targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator));
int index = 0;
for (; index < targetComponents.length && index < baseComponents.length; ++index) {
    if (!targetComponents[index].equals(baseComponents[index]))
        break;
StringBuilder result = new StringBuilder();
...
FilegetRelativeFile(File targetFile, File baseFile)
get Relative File
try {
    targetFile = targetFile.getCanonicalFile();
    baseFile = baseFile.getCanonicalFile();
} catch (IOException ignored) {
String pathSeparator = File.separator;
String basePath = baseFile.getAbsolutePath();
String normalizedTargetPath = normalize(targetFile.getAbsolutePath(), pathSeparator);
...
java.io.FilegetRelativeFile(java.io.File srcFile, int upCount, String... childPaths)
get Relative File
java.io.File rv = srcFile;
for (int i = 0; i < upCount; i++) {
    rv = rv.getParentFile();
for (String childPath : childPaths) {
    rv = new java.io.File(rv, childPath);
return rv;
...
FilegetRelativeFile(String baseDir, String fileName)
get Relative File
File f = new File(fileName);
if (!f.isAbsolute()) {
    f = new File(baseDir, fileName);
return f;
FilegetRelativeFileFromReference(String ref, File metadataFile)
get Relative File From Reference
String parentFilePath = "";
if (metadataFile.getParentFile() != null)
    parentFilePath = metadataFile.getParentFile().getPath();
File file = new File(new File(parentFilePath, ref).getCanonicalFile().toString()
        .replace(new File("").getCanonicalFile().toString(), ""));
return file;
StringgetRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize)
get Relative File Internal
ArrayList<String> basePath = getPathComponents(canonicalBaseFile);
ArrayList<String> pathToRelativize = getPathComponents(canonicalFileToRelativize);
if (!basePath.get(0).equals(pathToRelativize.get(0))) {
    return canonicalFileToRelativize.getPath();
int commonDirs;
StringBuilder sb = new StringBuilder();
for (commonDirs = 1; commonDirs < basePath.size() && commonDirs < pathToRelativize.size(); commonDirs++) {
...
StringgetRelativeFilename(File base, File target)
Simplistic version: return the substring after the base
return getRelativeFilename(base.getAbsolutePath(), target.getAbsolutePath());
StringgetRelativeFileName(File file, File basedir)
Gets a relative file from a filename against a base directory.
String canonicalFilePath = file.getCanonicalPath();
String canonicalBaseDirPath = basedir.getCanonicalPath();
if (canonicalFilePath.startsWith(canonicalBaseDirPath)) {
    int length = canonicalBaseDirPath.length();
    if (length < canonicalFilePath.length()) {
        return canonicalFilePath.substring(length + 1);
return null;
StringgetRelativeFileName(File target, File realativeTo)
Returns the name of one file relative to another.
LinkedList targetList = getPathList(target);
LinkedList relativeList = getPathList(realativeTo);
while (!targetList.isEmpty() && !relativeList.isEmpty()
        && targetList.getFirst().equals(relativeList.getFirst())) {
    targetList.removeFirst();
    relativeList.removeFirst();
StringBuffer fileName = new StringBuffer();
...