List of utility methods to do Path Relative Get
String | convertToRelativePath(String absolutePath, String relativeTo) taken from http://mrpmorris.blogspot.com/2007/05/convert-absolute-path-to-relative-path.html StringBuilder relativePath = null; absolutePath = absolutePath.replaceAll("\\\\", "/"); relativeTo = relativeTo.replaceAll("\\\\", "/"); if (absolutePath.equals(relativeTo) == true) { } else { String[] absoluteDirectories = absolutePath.split("/"); String[] relativeDirectories = relativeTo.split("/"); int length = absoluteDirectories.length < relativeDirectories.length ? absoluteDirectories.length ... |
String | getPathRelativeTo(File fileChild, File fileRoot) Returns that portion of the path of fileChild which is relative to the path of fileRoot. Check.arg().notNull(fileChild); Check.arg().notNull(fileRoot); if (fileChild.equals(fileRoot)) return ""; String pathChild = fileChild.getPath(); String pathRoot = fileRoot.getPath(); if (!pathChild.startsWith(pathRoot)) throw new IllegalArgumentException("fileChild = " + pathChild ... |
String | getRelativeFileName(File file, File basedir) Gets a relative file from a filename against a base directory. String canonicalFilePath = file.getCanonicalPath(); String canonicalBaseDirPath = basedir.getCanonicalPath(); if (canonicalFilePath.startsWith(canonicalBaseDirPath)) { int length = canonicalBaseDirPath.length(); if (length < canonicalFilePath.length()) { return canonicalFilePath.substring(length + 1); return null; |
String | getRelativePath(File parent, File file) get Relative Path return file.getAbsolutePath().substring(
parent.getAbsolutePath().length() + 1);
|
String | getWindowsRelativePath(String basedir, String path) get Windows Relative Path return getRelativePath(basedir, path, true);
|
String | relativePathFromBase(File file, File basedir) relative Path From Base String absolutePath = basedir.getAbsolutePath(); if (file.getAbsolutePath().startsWith(absolutePath) == false) { throw new IllegalArgumentException("File not in basedir: " + file); String relativePath = file.getAbsolutePath().substring( absolutePath.length()); return relativePath; ... |
String | getParentRelativeTo(File fileChild, File fileRoot) Returns a String that represents that part of fileChild's parent directory path which is contained inside fileRoot (that is, fileChild's parent's path relative to fileRoot). Check.arg().notNull(fileChild); Check.arg().notNull(fileRoot); File parent = fileChild.getParentFile(); Check.arg().notNull(parent); String pathParent = parent.getPath(); String pathRoot = fileRoot.getPath(); if (!pathParent.startsWith(pathRoot)) throw new IllegalArgumentException("fileChild's parent = " ... |