Java Utililty Methods Path Relative

List of utility methods to do Path Relative

Description

The list of methods to do Path Relative are organized into topic(s).

Method

StringgetRelativeUnixPath(File baseDir, File refFile)
get Relative Unix Path
String bpn = baseDir.getCanonicalPath().replace('\\', '/');
String rpn = refFile.getCanonicalPath().replace('\\', '/');
int len = Math.min(bpn.length(), rpn.length());
for (int i = 0, n = 0; i < len; i++) {
    char c = bpn.charAt(i);
    if (c == '/') {
        n = i + 1;
    } else if (c != rpn.charAt(i)) {
...
StringmakeRelativePath(File from, File to)
make Relative Path
File f = makeRelativeFile(from, to);
return (f != null ? f.getPath() : null);
StringmakeRelativePath(File parent, File file)
Find the relative path from some parent directory to a file nested within.
String filePath = file.getAbsolutePath();
String parentPath = parent.getAbsolutePath();
if (filePath.startsWith(parentPath)) {
    filePath = filePath.substring(parentPath.length() + 1);
return filePath;
IPathmakeRelativePath(IPath path, IPath relativeTo)
make Relative Path
int segments = matchingFirstSegments(relativeTo, path);
if (segments > 0) {
    IPath prefix = relativeTo.removeFirstSegments(segments);
    IPath suffix = path.removeFirstSegments(segments);
    IPath relativePath = new Path(""); 
    for (int i = 0; i < prefix.segmentCount(); ++i) {
        relativePath = relativePath.append(".." + IPath.SEPARATOR); 
    return relativePath.append(suffix);
return null;
StringmakeRelativePathAbsolute(String relativePath)
make Relative Path Absolute
File f = new File(relativePath);
String prefix = f.getAbsolutePath();
if (!prefix.endsWith(File.separator)) {
    prefix = prefix + File.separator;
int lastSlash = prefix.lastIndexOf(File.separator);
if (lastSlash != -1) {
    return prefix.substring(0, lastSlash + 1);
...
ListmakeRelativePaths(String classDir, List files)
make Relative Paths
List<String> relativePaths = new ArrayList<String>();
for (File file : files) {
    int prefixEnd = classDir.length() + 1;
    relativePaths.add(file.getPath().substring(prefixEnd));
return relativePaths;
IPathmakeRelativePathToIncludes(IPath fullPath, String[] includePaths)
make Relative Path To Includes
IPath relativePath = null;
int mostSegments = 0;
for (int i = 0; i < includePaths.length; ++i) {
    IPath includePath = new Path(includePaths[i]);
    if (isPrefix(includePath, fullPath)) {
        int segments = includePath.segmentCount();
        if (segments > mostSegments) {
            relativePath = fullPath.removeFirstSegments(segments).setDevice(null);
...