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

StringgetRelativePath(File root, File f)
get Relative Path
Path fPath = f.toPath().toAbsolutePath();
Path rootPath = root.toPath().toAbsolutePath();
if (fPath.startsWith(rootPath)) {
    return fPath.toString().substring(rootPath.toString().length() + 1);
} else {
    throw new IllegalStateException(fPath + " must be located inside " + rootPath);
PathgetRelativePath(final File file, final File baseDir)
Gets the relative from the specified directory to the specified file.
final Path fileDirPath = file.toPath();
final Path baseDirPath = baseDir.toPath();
return baseDirPath.relativize(fileDirPath);
StringgetRelativePath(Path file, Path baseDir)
get Relative Path
return getRelativePath(file != null ? file.toString() : null, baseDir != null ? baseDir.toString() : null);
StringgetRelativePath(Path rootPath, Path path)
Gets the relative path based on a root path and a target path.
String relativePath = "";
String r = rootPath.toString().trim();
String p = path.toString().trim();
if (!p.equalsIgnoreCase(r) && p.startsWith(r)) {
    relativePath = path.subpath(rootPath.getNameCount(), path.getNameCount()).toString();
return relativePath;
StringgetRelativePath(String file, String directory)
get Relative Path
Path pathAbsolute = Paths.get(file);
Path pathBase = Paths.get(directory);
Path pathRelative = pathBase.relativize(pathAbsolute);
return pathRelative.toString();
StringgetRelativePath(String fullPath, String homeFolderPath)
Return the path of the folder as in the database entry of the file
String parentPath = "";
try {
    URL parentURL = Paths.get(fullPath).toUri().toURL();
    URL homeFolderURL = Paths.get(homeFolderPath).toUri().toURL();
    parentPath = parentURL.getPath().replace(homeFolderURL.getPath(), "");
    parentPath = URLDecoder.decode(parentPath, "UTF-8");
    if (parentPath.length() > 0 && parentPath.charAt(parentPath.length() - 1) == '/') {
        parentPath = parentPath.substring(0, parentPath.length() - 1);
...
StringgetRelativePath(String relativePathFile)
get Relative Path
Path path = Paths.get(relativePathFile);
return path.getFileName().toFile().getAbsolutePath();
PathgetSourceFileRelativePath(Class declaringClass)
get Source File Relative Path
Path packageRelativePath = transformIntoPath(declaringClass);
String declaringFileName = getSourceFileName(declaringClass);
return packageRelativePath.resolve(declaringFileName);
booleanisRelativePath(Path baseDir, Path file)
is Relative Path
return file.startsWith(baseDir);
booleanisRelativized(final Path base, final String successor)
is Relativized
return isRelativized(base, Paths.get(successor));