List of utility methods to do Path Relative Get
String | getRelativePath(File child, File directory) get Relative Path String s1 = child.getAbsolutePath(); String s2 = directory.getAbsolutePath() + File.separator; int j = 0; for (int i = 0; i < s2.length(); i++) { if (s2.charAt(i) == s1.charAt(j)) { j++; } else { break; ... |
File | getRelativePath(File currentDir, File target) Constructs a relative path that addresses a given file target from a current directory. if (currentDir.isFile()) { currentDir = new File(currentDir.getParent()); if (!currentDir.isAbsolute() || !target.isAbsolute()) { return null; String[] dirParts = currentDir.toString().split("\\Q" + File.pathSeparator + "\\E"); String[] targetParts = target.toString().split("\\Q" + File.pathSeparator + "\\E"); ... |
String | getRelativePath(File dir, File child) Get relative path from two absolute paths. return dir.getAbsoluteFile().toURI().relativize(child.getAbsoluteFile().toURI()).getPath();
|
String | getRelativePath(File dir, File file) get Relative Path if (dir == null || file == null) { throw new IllegalArgumentException("dir and file arguments must not be null"); String dirname = dir.toURI().normalize().toString(); String filename = file.toURI().normalize().toString(); if (filename.length() < dirname.length() || !filename.startsWith(dirname)) { return null; if (filename.length() == dirname.length()) { return ""; return filename.substring(dirname.length()); |
String | getRelativePath(File f, File base) get Relative Path String fp = f.getAbsolutePath(); String bp = base.getAbsolutePath(); if (!fp.startsWith(bp)) { if (f.equals(base)) return ""; throw new IllegalArgumentException(f + "=" + fp + " is not a sub-file of " + base + "=" + bp); String rp = fp.substring(bp.length()); ... |
String | getRelativePath(File file) get Relative Path String curPath = new File(".").getCanonicalPath(); String targetPath = file.getCanonicalPath(); return targetPath.substring(curPath.length() + 1); |
String | getRelativePath(File file, File basedir) get Relative Path if (basedir == null) throw new IllegalArgumentException("basedir"); if (file == null) throw new IllegalArgumentException("file"); String path = file.getAbsolutePath(); int p = path.indexOf(basedir.getAbsolutePath()); if (p == -1) return path; ... |
String | getRelativePath(File file, File baseDir) get Relative Path if (isFileInDirRecursive(file, baseDir)) { return baseDir.toURI().relativize(file.toURI()).getPath(); } else { return getRelativePathRecursive(file, baseDir, ""); |
String | getRelativePath(File file, File baseDirectory) Given a file and base directory, the method returns file path relative to base directory. String baseDirAsString = baseDirectory.getCanonicalPath(); String filePathAsString = file.getCanonicalPath(); int dirIndex = filePathAsString.indexOf(baseDirAsString); if (dirIndex == -1) { throw new IllegalArgumentException("Directory path is not part of file path. directory = " + baseDirAsString + " scenario path = " + filePathAsString); return filePathAsString.substring(dirIndex + baseDirAsString.length() + 1); ... |
String | getRelativePath(File file, File directory) get Relative Path String relativePath = file.getPath().substring((int) directory.getPath().length()); if (relativePath.startsWith("/") || relativePath.startsWith("\\")) relativePath = relativePath.substring(1); return relativePath; |