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

StringcheckNormalizedRelative(String path)
Checks that the given path is relative and does not contain any .
Preconditions.checkArgument(!RELATIVISM.matcher(path).matches(),
        "path has unexpected . or .. components: %s", path);
Preconditions.checkArgument(!path.startsWith("/"), "path must be relative, but it starts with /: %s", path);
return path;
StringcombinePaths(String baseDir, String relativePath)
combine Paths
if (baseDir == null || baseDir.trim().isEmpty())
    return relativePath;
Path basePath = Paths.get(baseDir);
Path path = Paths.get(relativePath);
path = basePath.resolve(path);
path = path.normalize();
return path.toString();
Pathget(File basePath, String... relatives)
get
if (relatives == null || relatives.length == 0) {
    return basePath.toPath();
return Paths.get(basePath.getAbsolutePath(), relatives);
StringgetChildEntryRelativePath(Path base, Path child, boolean convertToLinuxPath)
get Child Entry Relative Path
String path = base.toUri().relativize(child.toUri()).getPath();
if (convertToLinuxPath && !"/".equals(base.getFileSystem().getSeparator())) {
    return path.replace(base.getFileSystem().getSeparator(), "/");
} else {
    return path;
FilegetFileFromBaseFileAndPathThatMayBeRelative(File baseDir, String pathThatMayBeRelative)
get File From Base File And Path That May Be Relative
if (Paths.get(pathThatMayBeRelative).isAbsolute()) {
    return new File(pathThatMayBeRelative);
} else {
    return Paths.get(baseDir.getAbsolutePath(), pathThatMayBeRelative).toFile().getCanonicalFile();
ListgetFileRelativeFolders(Path basePath, Path filePath)
get File Relative Folders
List<String> res = new ArrayList<>();
Path relativize = basePath.relativize(filePath).getParent();
if (relativize != null) {
    Iterator<Path> iterator = relativize.iterator();
    while (iterator.hasNext()) {
        res.add(iterator.next().toString());
return res;
OptionalgetOptionalPathRelativeToMavenProjectRoot(File absoluteFile)
get Optional Path Relative To Maven Project Root
if (!absoluteFile.isAbsolute()) {
    return Optional.of(absoluteFile);
File projectRoot = absoluteFile;
while (!isProjectRootDir(projectRoot) && projectRoot.getParentFile() != null) {
    projectRoot = projectRoot.getParentFile();
if (isProjectRootDir(projectRoot)) {
...
StringgetRelativeFilePathToCwd(File file)
get Relative File Path To Cwd
URI baseUri = Paths.get(".").toUri();
URI fileUri = file.toURI();
URI relativeUri = baseUri.relativize(fileUri);
return relativeUri.getPath();
StringgetRelativePath(File baseFile, File file, String resultIfImpossible)
get Relative Path
Path pathBase = Paths.get(baseFile.getAbsolutePath());
Path pathAbsolute = Paths.get(file.getAbsolutePath());
try {
    Path pathRelative = pathBase.relativize(pathAbsolute);
    return pathRelative.toString();
} catch (Exception e) {
    return resultIfImpossible;
StringgetRelativePath(File basePath, File path)
Determine the relative path between two files.
Path exactBase = Paths.get(getExactFile(basePath).toURI());
Path exactPath = Paths.get(getExactFile(path).toURI());
if (exactPath.startsWith(exactBase)) {
    return exactBase.relativize(exactPath).toString().replace('\\', '/');
return null;