Java Utililty Methods Path Relative Get

List of utility methods to do Path Relative Get

Description

The list of methods to do Path Relative Get are organized into topic(s).

Method

FilegetRelativePath(File src, File dst)
get Relative Path
String sep = System.getProperty("file.separator");
String relativePath;
String temp;
int index;
ArrayList directoryList1 = new ArrayList();
ArrayList directoryList2 = new ArrayList();
temp = src.toString();
while (temp.indexOf(sep) > -1) {
...
StringgetRelativePath(File subj, File relativeTo)
Gets the relative representations of a file compared to another.
String subjPath = subj.getAbsolutePath();
String relativeToPath = relativeTo.getAbsolutePath();
if (!subj.isDirectory()) {
    int idx = subjPath.lastIndexOf(File.separator);
    if (idx != -1) {
        subjPath = subjPath.substring(0, idx);
if (!relativeTo.isDirectory()) {
    int idx = relativeToPath.lastIndexOf(File.separator);
    if (idx != -1) {
        relativeToPath = relativeToPath.substring(0, idx);
StringTokenizer subjPathTok = new StringTokenizer(subjPath, File.separator);
StringTokenizer relativeToPathTok = new StringTokenizer(relativeToPath, File.separator);
String subjTok = null;
String relativeToTok = null;
while (subjPathTok.hasMoreTokens() && relativeToPathTok.hasMoreTokens()) {
    subjTok = subjPathTok.nextToken();
    relativeToTok = relativeToPathTok.nextToken();
    if (!subjTok.equals(relativeToTok)) {
        break;
StringBuilder relPath = new StringBuilder();
if (!subjTok.equals(relativeToTok)) {
    relPath.append("..");
    relPath.append(File.separator);
while (relativeToPathTok.hasMoreTokens()) {
    relativeToPathTok.nextToken();
    relPath.append("..");
    relPath.append(File.separator);
if (!subjTok.equals(relativeToTok)) {
    relPath.append(subjTok);
    relPath.append(File.separator);
while (subjPathTok.hasMoreTokens()) {
    subjTok = subjPathTok.nextToken();
    relPath.append(subjTok);
    relPath.append(File.separator);
relPath.append(subj.getName());
return relPath.toString();
StringgetRelativePath(File target, File base)
Returns the path of one File relative to another.
String[] baseComponents;
String[] targetComponents;
baseComponents = getFullPath(base).split(Pattern.quote(SEPARATOR));
targetComponents = getFullPath(target).split(Pattern.quote(SEPARATOR));
int index = 0;
for (; index < targetComponents.length && index < baseComponents.length; ++index) {
    if (!targetComponents[index].equals(baseComponents[index]))
        break;
...
StringgetRelativePath(File target, File relativeTo)
get Relative Path
List<File> targetPaths = new ArrayList<File>();
List<File> relativePaths = new ArrayList<File>();
generateList(target, targetPaths);
generateList(relativeTo, relativePaths);
String result = "";
List<File> namedPaths = new ArrayList<File>();
namedPaths.addAll(targetPaths);
namedPaths.removeAll(relativePaths);
...
StringgetRelativePath(File wd, File file)
get Relative Path
int skip = file.getPath().length() > wd.getPath().length() ? 1 : 0;
String relName = file.getPath().substring(wd.getPath().length() + skip);
if (File.separatorChar != '/') {
    relName = relName.replace(File.separatorChar, '/');
return relName;
StringgetRelativePath(final File base, final File child)
get Relative Path
if (base == null || child == null) {
    throw new IllegalArgumentException("Cannot be null.");
return base.toURI().relativize(child.toURI()).getPath();
StringgetRelativePath(final File base, final File name)
get Relative Path
File parent = base.getParentFile();
if (parent == null)
    return name.getPath();
String bpath = base.getCanonicalPath();
String fpath = name.getCanonicalPath();
if (fpath.startsWith(bpath)) {
    return fpath.substring(bpath.length() + 1);
} else {
...
StringgetRelativePath(final File baseDirectory, final File f)
get Relative Path
String s = f.getAbsolutePath();
String base = baseDirectory.getAbsolutePath();
if (s.startsWith(base)) {
    s = s.substring(base.length());
return s;
StringgetRelativePath(final File basePathFile, final File pathFile)
Get pathFile 's relative path from basePathFile .
String basePath = normalizePath(basePathFile.getAbsolutePath());
String path = normalizePath(pathFile.getAbsolutePath());
if (path.startsWith(basePath)) {
    String relPath = path.substring(basePath.length(), path.length());
    if (relPath.startsWith("/")) {
        relPath = relPath.substring(1);
    return relPath;
...
StringgetRelativePath(final File file, final File folder)
Returns file path relative to specified folder or canonical path if file is not inside that folder.
return folder.toURI().relativize(file.toURI()).getPath();