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(final File from, final File to)
get Relative Path
return getRelativePath(from, to, File.separatorChar);
StringgetRelativePath(final File fromFile, final File toFile)
get Relative Path
final String[] fromSegments = getReversePathSegments(fromFile);
final String[] toSegments = getReversePathSegments(toFile);
String relativePath = "";
int i = fromSegments.length - 1;
int j = toSegments.length - 1;
while ((i >= 0) && (j >= 0) && (fromSegments[i].equals(toSegments[j]))) {
    i--;
    j--;
...
StringgetRelativePath(final File parentDir, final File file)
get Relative Path
if (file.equals(parentDir)) {
    return file.getName();
} else {
    return file.getAbsolutePath().substring(parentDir.getAbsolutePath().length() + 1);
StringgetRelativePath(final File parentDirectory, final File file)
Return the relative path of the file from the parentDirectory.
final String parentPath = getCanonicalPath(parentDirectory);
final String filePath = getCanonicalPath(file);
if (filePath.startsWith(parentPath)) {
    return filePath.substring(parentPath.length() + 1);
return filePath;
StringgetRelativePath(final String base, final File targetFile)
Returns a relative path for the targetFile relative to the base directory.
try {
    String canonicalBase = base;
    if (base.charAt(base.length() - 1) != File.separatorChar) {
        canonicalBase = base + File.separatorChar;
    String canonicalTarget;
    if (System.getProperty("os.name").equals("OS/400"))
        canonicalTarget = targetFile.getPath();
...
StringgetRelativePath(String base, String fullPath)
Returns fullPath relative to base if base is indeed a sudirectory of fullpath.
if (fullPath == null)
    return null;
if (base == null || !fullPath.startsWith(base)) {
    return fullPath;
String result = fullPath.substring(base.length());
if (result.length() == 0) {
    return ".";
...
StringgetRelativePath(String base, String relative, boolean isBaseFile)
Returns the path corresponding to the given base path, and the given relative path - results in "concatenation" of both, sort of.
String toReturn;
String compared = base.replace('\\', '/');
if (isBaseFile) {
    int idx;
    if ((idx = compared.lastIndexOf('/')) >= 0) {
        toReturn = base.substring(0, idx) + File.separator + relative;
    } else {
        toReturn = base + File.separator + relative;
...
StringgetRelativePath(String baseFullPath, File file)
get Relative Path
return getRelativePath(baseFullPath, getFullPath(file));
StringgetRelativePath(String basePath, String path)
Get the relative path from the canoical basepath and path.
if (basePath == null || path == null || basePath.equalsIgnoreCase(path))
    return "";
else if (path.startsWith(basePath)) {
    String relPath = path.substring(basePath.length(), path.length());
    return "." + relPath;
return "";
StringgetRelativePath(String basePath, String pathToRelativize)
get Relative Path
File baseFile = new File(basePath);
if (baseFile.isFile()) {
    baseFile = baseFile.getParentFile();
return getRelativeFileInternal(baseFile.getCanonicalFile(), new File(pathToRelativize).getCanonicalFile());