List of utility methods to do Path Relative nio
| String | checkNormalizedRelative(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;
|
| String | combinePaths(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(); |
| Path | get(File basePath, String... relatives) get if (relatives == null || relatives.length == 0) { return basePath.toPath(); return Paths.get(basePath.getAbsolutePath(), relatives); |
| String | getChildEntryRelativePath(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; |
| File | getFileFromBaseFileAndPathThatMayBeRelative(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(); |
| List | getFileRelativeFolders(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; |
| Optional | getOptionalPathRelativeToMavenProjectRoot(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)) { ... |
| String | getRelativeFilePathToCwd(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(); |
| String | getRelativePath(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; |
| String | getRelativePath(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; |