Java Utililty Methods Path Relative nio

List of utility methods to do Path Relative nio

Description

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

Method

StringrelativePath(String input, String mainurl)
Compute sub directory of given input, relative to main module.
Path inputPath = Paths.get(input);
Path rootPath = Paths.get(mainurl);
Path relativize = rootPath.getParent().relativize(inputPath.getParent());
return relativize.toString();
StringrelativePathTo(Path path, Path basePath)
relative Path To
if ("index.html".equalsIgnoreCase(path.getFileName().toString())) {
    path = path.getParent();
if (!java.nio.file.Files.isDirectory(basePath)) {
    basePath = Optional.ofNullable(basePath.getParent()).orElse(basePath);
String relUrl = basePath.relativize(path).toString();
return "".equals(relUrl) ? "." : relUrl;
...
Pathrelativize(Path absoluteDirectory, Path subDirectory)
relativize
return absoluteDirectory.relativize(subDirectory.toAbsolutePath().normalize());
Listrelativize(Path target, Collection paths)
relativize
List<Path> relativizedPaths = new ArrayList<>();
for (Path path : paths) {
    relativizedPaths.add(target.relativize(path));
return relativizedPaths;
StringrelativizeAndNormalizePath(final String baseDirectory, final String path)
Constructs a normalized relative path between base directory and a given path.
if (baseDirectory == null) {
    return path;
final Path pathAbsolute = Paths.get(path).normalize();
final Path pathBase = Paths.get(baseDirectory).normalize();
return pathBase.relativize(pathAbsolute).toString();
IPathrelativizePath(IPath path, IPath basePath)
Returns the relative path of the original path with respect to the basePath if they share the same prefix path, otherwise path is returned unchanged.
if (path == null) {
    return basePath;
if (basePath == null) {
    return path;
java.nio.file.Path base = basePath.toFile().toPath().toAbsolutePath();
java.nio.file.Path child = path.toFile().toPath().toAbsolutePath();
...
IPathrelativizePath(IPath path, IPath basePath, boolean strict)
relativize Path
if (path == null) {
    return basePath;
if (basePath == null) {
    return path;
java.nio.file.Path base = basePath.toFile().toPath().toAbsolutePath();
java.nio.file.Path child = path.toFile().toPath().toAbsolutePath();
...
StringrelativizePath(Path root, Path child)
relativize Path
String childPath = child.toAbsolutePath().toString();
String rootPath = root.toAbsolutePath().toString();
if (childPath.equals(rootPath)) {
    return "";
int indexOfRootInChild = childPath.indexOf(rootPath);
if (indexOfRootInChild != 0) {
    throw new IllegalArgumentException(
...
StringrelativizePath(String basePath, File toRelativize)
This method has been deprecated use the Java Path relativize method instead.
if (toRelativize == null) {
    return null;
if (basePath == null || !toRelativize.getPath().startsWith(basePath)) {
    return toRelativize.getPath();
Path path = FileSystems.getDefault().getPath(basePath);
Path relativePath = path.relativize(toRelativize.toPath());
...
StringresolvePathIfRelativeTo(File referenceFile, String partialPath)
resolve Path If Relative To
if (partialPath.startsWith("..") || partialPath.startsWith(".")) {
    String definitionOriginDir = referenceFile.getParentFile().getAbsolutePath();
    partialPath = Paths.get(definitionOriginDir, partialPath).toAbsolutePath().toString();
return partialPath;