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 parentDirectory, File file)
Returns the path of the given file relative to the parent directory
String relativePath = null;
String parentPath = parentDirectory.getPath();
String childPath = file.getPath();
if (childPath.startsWith(parentPath + File.separator)) {
    relativePath = childPath.substring(parentPath.length() + 1);
relativePath = relativePath.replaceAll("\\\\", "/");
return relativePath;
...
StringgetRelativePath(File path, File base)
The base attribute specifies what the directory base the relative path should be considered relative to.
String path_abs = path.getAbsolutePath();
String base_abs = base.getAbsolutePath();
int idx = path_abs.indexOf(base_abs);
if (idx == -1) {
    throw new IllegalArgumentException(
            "Path (" + path_abs + ") " + "does not contain " + "base (" + base_abs + ")");
String relativePath = "." + path_abs.substring(idx + base_abs.length());
...
StringgetRelativePath(File path, File basePath)
get Relative Path
try {
    String dir = path.toURL().toExternalForm();
    String baseDir = appendSeparator(basePath.toURL().toExternalForm(), "/"); 
    StringBuffer result = new StringBuffer();
    while (dir.indexOf(baseDir) == -1) {
        basePath = basePath.getParentFile();
        baseDir = appendSeparator(basePath.toURL().toExternalForm(), "/"); 
        result.append("../"); 
...
StringgetRelativePath(File ref_file, File tst_file)
get Relative Path
File[] roots = getFileSystemRoots();
String ref_path = null;
String tst_path = null;
try {
    ref_path = ref_file.getCanonicalPath();
    tst_path = tst_file.getCanonicalPath();
} catch (IOException e) {
    ref_path = ref_file.getAbsolutePath();
...
StringgetRelativePath(File root, File file)
gets the relative path of a file.
String rel = file.getAbsolutePath().substring(root.getAbsolutePath().length() + 1);
rel = rel.replaceAll("\\\\", "/");
return rel;
StringgetRelativePath(File root, File file)
Determines the root relative path of file in a platform independent manner.
ArrayList<String> segments = new ArrayList<String>();
if (root.isFile()) {
    root = root.getParentFile();
getPathSegments(root, file, segments);
StringBuilder path = new StringBuilder();
for (int p = 0; p < segments.size(); p++) {
    if (p > 0) {
...
StringgetRelativePath(File root, File target, String fileSeparator)
get Relative Path
if (fileSeparator == null)
    fileSeparator = File.separator;
Vector rootHierarchy = new Vector();
for (File dir = root; dir != null; dir = dir.getParentFile())
    rootHierarchy.add(dir);
StringBuffer buf = new StringBuffer();
for (; target != null; target = target.getParentFile()) {
    int idx = rootHierarchy.indexOf(target);
...
StringgetRelativePath(File rootDirectory, File file)
Returns the path of file relative to rootDirectory .
try {
    String rootDir = rootDirectory.getCanonicalPath() + File.separator;
    String f = file.getCanonicalPath();
    if (f.startsWith(rootDir)) {
        return f.substring(rootDir.length());
} catch (IOException e) {
return file.getPath();
StringgetRelativePath(File source, File destination)
Gets a relative path from the source file to the destination
String sourceDir = null;
String destDir = null;
if (source.isDirectory()) {
    sourceDir = source.getCanonicalPath();
} else {
    sourceDir = source.getParentFile().getCanonicalPath();
if (destination.isDirectory()) {
...
StringgetRelativePath(File source, File target)
Get a relative path pointing from one File to another.
source = new File(source.getCanonicalPath());
target = new File(target.getCanonicalPath());
File common = getCommonAncestor(source, target);
if (common == null) {
    return null;
String upPath = getRelativeAncestor(common, source);
String downPath = getRelativeDescendant(common, target);
...