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 file, File folder)
get Relative Path
String filePath = file.getAbsolutePath();
String folderPath = folder.getAbsolutePath();
if (filePath.startsWith(folderPath)) {
    return filePath.substring(folderPath.length() + 1);
} else {
    return null;
StringgetRelativePath(File file, File root)
Strips off the part of the path before the start of the root file.
int rootLength = root.getAbsolutePath().length();
String absolutePath = file.getAbsolutePath();
String relativePath = absolutePath.substring(rootLength + 1);
return relativePath;
StringgetRelativePath(File file, File root)
Si root="/export/share-img y file="/export/share-img/2009/03/01/file.txt" esto devuelve la cadena de caracteres "/2009/03/01/file.txt".
if (root == null) {
    return file.getAbsolutePath();
if (file == null) {
    return null;
String rootAbsolutePath = root.getAbsolutePath();
String fileAbsolutePath = file.getAbsolutePath();
...
StringgetRelativePath(File file, File srcDir)
Returns the relative path.
String base = srcDir.getPath();
String filePath = file.getPath();
String relativePath = new File(base).toURI().relativize(new File(filePath).toURI()).getPath();
return relativePath;
StringgetRelativePath(File file, String xmlDirectoryPath)
Returns a part of absolute path that starts from the xml directory path (xml directory path is cut off).
return removeLeadingSlashes(file.getAbsolutePath().substring(xmlDirectoryPath.length()));
StringgetRelativePath(File fileOrFolder, File baseFolder)
get Relative Path
if (!baseFolder.isDirectory())
    baseFolder = baseFolder.getAbsoluteFile().getParentFile();
return baseFolder.toURI().relativize(fileOrFolder.toURI()).getPath();
StringgetRelativePath(File folderContainingFile, File fileInFolder)
get Relative Path
String filePath = fileInFolder.getCanonicalPath();
String folderPath = folderContainingFile.getCanonicalPath();
return new File(folderPath).toURI().relativize(new File(filePath).toURI()).getPath();
StringgetRelativePath(File fromDir, File toFile)
get the path to a given file relative to a given directory
String fromStr = fromDir.getAbsolutePath();
if (!fromStr.endsWith(File.separator)) {
    fromStr = fromStr + File.separator;
String toStr = toFile.getAbsolutePath();
int pos = fromStr.indexOf(File.separator);
int fromLen = fromStr.length();
int toLen = toStr.length();
...
StringgetRelativePath(File fromFile, File toFile)
Calculates the relative path between two files.
String fromPath = fromFile.getCanonicalPath();
String toPath = toFile.getCanonicalPath();
String[] fromPathStack = getPathStack(fromPath);
String[] toPathStack = getPathStack(toPath);
if (0 < toPathStack.length && 0 < fromPathStack.length) {
    if (!fromPathStack[0].equals(toPathStack[0])) {
        return getPath(Arrays.asList(toPathStack));
} else {
    return getPath(Arrays.asList(toPathStack));
int minLength = Math.min(fromPathStack.length, toPathStack.length);
int same = 1; 
for (; same < minLength && fromPathStack[same].equals(toPathStack[same]); same++) {
List relativePathStack = new ArrayList();
for (int i = same; i < fromPathStack.length; i++) {
    relativePathStack.add("..");
for (int i = same; i < toPathStack.length; i++) {
    relativePathStack.add(toPathStack[i]);
return getPath(relativePathStack);
StringgetRelativePath(File fromFile, File toFile)
get Relative Path
if (toFile != null) {
    ArrayList<File> lst = getParents(fromFile, null);
    if (fromFile.isDirectory()) {
        lst.add(fromFile);
    File rootFile = getRoot(toFile, lst);
    if (rootFile != null) {
        String ret = toRoot(rootFile, fromFile)
...