Java Path Relative nio relativizeAndNormalizePath(final String baseDirectory, final String path)

Here you can find the source of relativizeAndNormalizePath(final String baseDirectory, final String path)

Description

Constructs a normalized relative path between base directory and a given path.

License

Open Source License

Parameter

Parameter Description
baseDirectory the base path to which given path is relativized
path the path to relativize against base directory

Return

the relative normalized path between base directory and path or path if base directory is null

Declaration

public static String relativizeAndNormalizePath(final String baseDirectory, final String path) 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**//from  w  w  w .  j  av  a  2s .c om
     * Constructs a normalized relative path between base directory and a given path.
     * @param baseDirectory the base path to which given path is relativized
     * @param path the path to relativize against base directory
     * @return the relative normalized path between base directory and path or path if base
     * directory is null
     */
    public static String relativizeAndNormalizePath(final String baseDirectory, final String 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();
    }
}

Related

  1. isRelativized(final Path base, final String successor)
  2. relativePath(String input, String mainurl)
  3. relativePathTo(Path path, Path basePath)
  4. relativize(Path absoluteDirectory, Path subDirectory)
  5. relativize(Path target, Collection paths)
  6. relativizePath(IPath path, IPath basePath)
  7. relativizePath(IPath path, IPath basePath, boolean strict)
  8. relativizePath(Path root, Path child)
  9. relativizePath(String basePath, File toRelativize)