Java Path Relative Get getRelativePath(File base, File file)

Here you can find the source of getRelativePath(File base, File file)

Description

get Relative Path

License

LGPL

Declaration

public static String getRelativePath(File base, File file) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

import java.io.IOException;

public class Main {
    public static String getRelativePath(File base, File file) throws IOException {
        String basePath = base.getCanonicalPath();
        String filePath = file.getCanonicalPath();

        return getRelativePath(basePath, filePath);
    }/*from  ww w  .  j a  v a  2 s. com*/

    public static String getRelativePath(String basePath, String filePath) {
        if (basePath.equals(filePath))
            return "";

        // Lets consider one type of File Separator.
        basePath = normalizePath(basePath);
        filePath = normalizePath(filePath);

        int beginIndex = filePath.indexOf(basePath);
        if (beginIndex == -1)
            return filePath;

        beginIndex += basePath.length() + 1;

        return filePath.substring(beginIndex);
    }

    public static String normalizePath(String path) {
        String separator = "/";
        return path.replaceAll("\\\\", separator);
    }
}

Related

  1. getRelativePath(File ancestor, File file)
  2. getRelativePath(File base, File absoluteFile)
  3. getRelativePath(File base, File file)
  4. getRelativePath(File base, File file)
  5. getRelativePath(File base, File file)
  6. getRelativePath(File base, File name)
  7. getRelativePath(File base, File name)
  8. getRelativePath(File base, File path)
  9. getRelativePath(File base, File path)