List of utility methods to do Path Relative
File | getRelativeFile(File target, File base) Returns the path of one file relative to another. return new File(getRelativePath(target.getAbsolutePath(), base.getAbsolutePath())); |
File | getRelativeFile(File target, File baseDirectory) From http://stackoverflow.com /a/1269907. String[] baseComponents = baseDirectory.getCanonicalPath().split(Pattern.quote(File.separator)); String[] targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator)); int index = 0; for (; index < targetComponents.length && index < baseComponents.length; ++index) { if (!targetComponents[index].equals(baseComponents[index])) break; StringBuilder result = new StringBuilder(); ... |
File | getRelativeFile(File targetFile, File baseFile) get Relative File try { targetFile = targetFile.getCanonicalFile(); baseFile = baseFile.getCanonicalFile(); } catch (IOException ignored) { String pathSeparator = File.separator; String basePath = baseFile.getAbsolutePath(); String normalizedTargetPath = normalize(targetFile.getAbsolutePath(), pathSeparator); ... |
java.io.File | getRelativeFile(java.io.File srcFile, int upCount, String... childPaths) get Relative File java.io.File rv = srcFile; for (int i = 0; i < upCount; i++) { rv = rv.getParentFile(); for (String childPath : childPaths) { rv = new java.io.File(rv, childPath); return rv; ... |
File | getRelativeFile(String baseDir, String fileName) get Relative File File f = new File(fileName); if (!f.isAbsolute()) { f = new File(baseDir, fileName); return f; |
File | getRelativeFileFromReference(String ref, File metadataFile) get Relative File From Reference String parentFilePath = ""; if (metadataFile.getParentFile() != null) parentFilePath = metadataFile.getParentFile().getPath(); File file = new File(new File(parentFilePath, ref).getCanonicalFile().toString() .replace(new File("").getCanonicalFile().toString(), "")); return file; |
String | getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize) get Relative File Internal ArrayList<String> basePath = getPathComponents(canonicalBaseFile); ArrayList<String> pathToRelativize = getPathComponents(canonicalFileToRelativize); if (!basePath.get(0).equals(pathToRelativize.get(0))) { return canonicalFileToRelativize.getPath(); int commonDirs; StringBuilder sb = new StringBuilder(); for (commonDirs = 1; commonDirs < basePath.size() && commonDirs < pathToRelativize.size(); commonDirs++) { ... |
String | getRelativeFilename(File base, File target) Simplistic version: return the substring after the base return getRelativeFilename(base.getAbsolutePath(), target.getAbsolutePath());
|
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 | getRelativeFileName(File target, File realativeTo) Returns the name of one file relative to another. LinkedList targetList = getPathList(target); LinkedList relativeList = getPathList(realativeTo); while (!targetList.isEmpty() && !relativeList.isEmpty() && targetList.getFirst().equals(relativeList.getFirst())) { targetList.removeFirst(); relativeList.removeFirst(); StringBuffer fileName = new StringBuffer(); ... |