Java Path Relative nio relativizePath(Path root, Path child)

Here you can find the source of relativizePath(Path root, Path child)

Description

relativize Path

License

Apache License

Declaration

public static String relativizePath(Path root, Path child) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.nio.file.*;

public class Main {
    public static String relativizePath(Path root, Path child) {
        String childPath = child.toAbsolutePath().toString();
        String rootPath = root.toAbsolutePath().toString();
        if (childPath.equals(rootPath)) {
            return "";
        }//from  ww w .j  a  va 2 s .  c  o  m
        int indexOfRootInChild = childPath.indexOf(rootPath);
        if (indexOfRootInChild != 0) {
            throw new IllegalArgumentException(
                    "Child path " + childPath + "is not beginning with root path " + rootPath);
        }
        String relativizedPath = childPath.substring(rootPath.length(), childPath.length());
        while (relativizedPath.startsWith(root.getFileSystem().getSeparator())) {
            relativizedPath = relativizedPath.substring(1);
        }
        return relativizedPath;
    }
}

Related

  1. relativize(Path absoluteDirectory, Path subDirectory)
  2. relativize(Path target, Collection paths)
  3. relativizeAndNormalizePath(final String baseDirectory, final String path)
  4. relativizePath(IPath path, IPath basePath)
  5. relativizePath(IPath path, IPath basePath, boolean strict)
  6. relativizePath(String basePath, File toRelativize)
  7. resolvePathIfRelativeTo(File referenceFile, String partialPath)
  8. resolveRelative(Path p, String other)
  9. resolveRelativePath(String first, String... more)