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 child, File directory)
get Relative Path
String s1 = child.getAbsolutePath();
String s2 = directory.getAbsolutePath() + File.separator;
int j = 0;
for (int i = 0; i < s2.length(); i++) {
    if (s2.charAt(i) == s1.charAt(j)) {
        j++;
    } else {
        break;
...
FilegetRelativePath(File currentDir, File target)
Constructs a relative path that addresses a given file target from a current directory.
if (currentDir.isFile()) {
    currentDir = new File(currentDir.getParent());
if (!currentDir.isAbsolute() || !target.isAbsolute()) {
    return null;
String[] dirParts = currentDir.toString().split("\\Q" + File.pathSeparator + "\\E");
String[] targetParts = target.toString().split("\\Q" + File.pathSeparator + "\\E");
...
StringgetRelativePath(File dir, File child)
Get relative path from two absolute paths.
return dir.getAbsoluteFile().toURI().relativize(child.getAbsoluteFile().toURI()).getPath();
StringgetRelativePath(File dir, File file)
get Relative Path
if (dir == null || file == null) {
    throw new IllegalArgumentException("dir and file arguments must not be null");
String dirname = dir.toURI().normalize().toString();
String filename = file.toURI().normalize().toString();
if (filename.length() < dirname.length() || !filename.startsWith(dirname)) {
    return null;
if (filename.length() == dirname.length()) {
    return "";
return filename.substring(dirname.length());
StringgetRelativePath(File f, File base)
get Relative Path
String fp = f.getAbsolutePath();
String bp = base.getAbsolutePath();
if (!fp.startsWith(bp)) {
    if (f.equals(base))
        return ""; 
    throw new IllegalArgumentException(f + "=" + fp + " is not a sub-file of " + base + "=" + bp);
String rp = fp.substring(bp.length());
...
StringgetRelativePath(File file)
get Relative Path
String curPath = new File(".").getCanonicalPath();
String targetPath = file.getCanonicalPath();
return targetPath.substring(curPath.length() + 1);
StringgetRelativePath(File file, File basedir)
get Relative Path
if (basedir == null)
    throw new IllegalArgumentException("basedir");
if (file == null)
    throw new IllegalArgumentException("file");
String path = file.getAbsolutePath();
int p = path.indexOf(basedir.getAbsolutePath());
if (p == -1)
    return path;
...
StringgetRelativePath(File file, File baseDir)
get Relative Path
if (isFileInDirRecursive(file, baseDir)) {
    return baseDir.toURI().relativize(file.toURI()).getPath();
} else {
    return getRelativePathRecursive(file, baseDir, "");
StringgetRelativePath(File file, File baseDirectory)
Given a file and base directory, the method returns file path relative to base directory.
String baseDirAsString = baseDirectory.getCanonicalPath();
String filePathAsString = file.getCanonicalPath();
int dirIndex = filePathAsString.indexOf(baseDirAsString);
if (dirIndex == -1) {
    throw new IllegalArgumentException("Directory path is not part of file path. directory = "
            + baseDirAsString + " scenario path = " + filePathAsString);
return filePathAsString.substring(dirIndex + baseDirAsString.length() + 1);
...
StringgetRelativePath(File file, File directory)
get Relative Path
String relativePath = file.getPath().substring((int) directory.getPath().length());
if (relativePath.startsWith("/") || relativePath.startsWith("\\"))
    relativePath = relativePath.substring(1);
return relativePath;