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(@Nullable File projectFile, @Nullable File rootFile)
get Relative Path
String returnPath = null;
if (projectFile != null && rootFile != null) {
    returnPath = getRelativePath(projectFile.getAbsolutePath(), rootFile.getAbsolutePath());
return returnPath;
StringgetRelativePath(File a, File b, String pathSep)
get Relative Path
return getRelativePath(a.getAbsolutePath(), b.getAbsolutePath(), pathSep);
StringgetRelativePath(File ancestor, File file)
get Relative Path
return getRelativePath(ancestor, file, "/");
FilegetRelativePath(File base, File absoluteFile)
get Relative Path
checkAbsolutePath(base);
checkAbsolutePath(absoluteFile);
try {
    File res = base.toPath().relativize(absoluteFile.toPath()).toFile();
    return new File('.' + File.separator + res.getPath());
} catch (Exception ignored) {
String[] basePath = getPathParts(base);
...
StringgetRelativePath(File base, File file)
Get relative path between two files http://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls
return base.toURI().relativize(file.toURI()).getPath();
StringgetRelativePath(File base, File file)
returns the relative path of file to base.
String filePath = file.getAbsoluteFile().getCanonicalPath();
String basePath = base.getAbsoluteFile().getCanonicalPath();
int start = filePath.indexOf(basePath);
if (start != 0)
    throw new IOException(basePath + " not ancestor of " + filePath);
return filePath.substring(basePath.length());
StringgetRelativePath(File base, File file)
get Relative Path
String basePath = base.getCanonicalPath();
String filePath = file.getCanonicalPath();
return getRelativePath(basePath, filePath);
FilegetRelativePath(File base, File file)
Finds the relative path from base to file.
try {
    String absBase = base.getCanonicalPath();
    String absFile = file.getCanonicalPath();
    if (absBase.equals(absFile)) { 
        return new File("");
    int divergenceCharIndex = 0;
    int divergenceSeperatorIndex = 0;
...
StringgetRelativePath(File base, File name)
Computes the path for a file relative to a given base, or fails if the only shared directory is the root and the absolute form is better.
File parent = base.getParentFile();
if (parent == null) {
    throw new IOException("No common directory");
String bpath = base.getCanonicalPath();
String fpath = name.getCanonicalPath();
if (fpath.startsWith(bpath)) {
    return fpath.substring(bpath.length() + 1);
...
StringgetRelativePath(File base, File name)
Computes the path for a file relative to a given base, or fails if the only shared directory is the root and the absolute form is better.
if (base != null && name != null) {
    File parent = base.getParentFile();
    if (name.getName().length() > 0) {
        try {
            if (parent == null) {
                return name.getAbsolutePath();
            String bpath = base.getCanonicalPath();
...