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 base, File path)
get Relative Path
return base.toPath().relativize(path.toPath()).toString();
StringgetRelativePath(File base, File path)
get Relative Path
try {
    String relative = base.getCanonicalFile().toURI().relativize(path.getCanonicalFile().toURI()).getPath();
    return relative;
} catch (IOException e) {
    return null;
StringgetRelativePath(File base, File target)
Returns the relative path from base to target.
String baseString = base.getAbsolutePath().replace('\\', '/');
String targetString = target.getAbsolutePath().replace('\\', '/');
String commonPrefix = findGreatestCommonPrefix(baseString, targetString);
if (commonPrefix.length() == 0) {
    throw new IllegalArgumentException("Arguments must have common prefix");
String relativePath = targetString.substring(commonPrefix.length());
if (commonPrefix.length() == baseString.length()) {
...
StringgetRelativePath(File baseDir, File file)
get Relative Path
if (baseDir.equals(file))
    return "";
if (baseDir.getParentFile() == null)
    return file.getAbsolutePath().substring(baseDir.getAbsolutePath().length());
return file.getAbsolutePath().substring(baseDir.getAbsolutePath().length() + 1);
StringgetRelativePath(File baseDir, File file)
get Relative Path
String filePath = file.getAbsolutePath();
String basePath = baseDir.getAbsolutePath();
String relativePath = filePath.substring(basePath.length());
if (relativePath.charAt(0) == File.separatorChar) {
    relativePath = relativePath.substring(1);
return relativePath;
StringgetRelativePath(File baseDir, File file)
get Relative Path
String basePath = baseDir.getAbsolutePath();
if (!basePath.endsWith(File.separator)) {
    basePath += File.separator;
String filePath = file.getAbsolutePath();
if (!filePath.startsWith(basePath)) {
    throw new IllegalArgumentException("Not dir-relative: " + filePath + " vs " + basePath);
String relativePath = filePath.substring(basePath.length());
return relativePath;
StringgetRelativePath(File baseDir, File file)
Returns the relative path of file to the file basedir.
final String base = baseDir.getCanonicalPath();
String fileName = file.getCanonicalPath();
if (fileName.startsWith(base)) {
    fileName = fileName.substring(base.length());
    if (fileName.charAt(0) == '/') {
        fileName = fileName.substring(1);
} else {
...
StringgetRelativePath(File baseDir, File subFile, String seperator)
get Relative Path
String basePath = baseDir.getAbsolutePath();
String filePath = subFile.getAbsolutePath();
if (!filePath.startsWith(basePath)) {
    return null;
String relativePath = filePath.substring(basePath.length() + 1);
return seperator == null ? relativePath
        : relativePath.replaceAll("\\\\", seperator).replaceAll("/", seperator);
...
StringgetRelativePath(File baseFile, File file)
get Relative Path
String basePath = "";
String path = "";
basePath = baseFile.getAbsolutePath();
path = file.getAbsolutePath();
if (!basePath.endsWith(File.separator)) {
    basePath = basePath + File.separator;
if (path.indexOf(basePath) != 0)
...
StringgetRelativePath(File baseFolder, File subject)
Helper function to get the relative path for subject starting at baseFolder
return subject.getAbsolutePath().substring(baseFolder.getAbsolutePath().length() + 1);