Java Relative Path Get relativePath(File parent, File child)

Here you can find the source of relativePath(File parent, File child)

Description

get relative path of child to parent.
To be noticed, relative returned NOT start with "/" and file separator will be replaced by "/"

License

Open Source License

Parameter

Parameter Description
parent a parameter
child a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static final String relativePath(File parent, File child) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.IOException;

public class Main {
    /**//ww  w  .  ja  v a  2s .co  m
     * get relative path of <i>child</i> to <i>parent</i>.<br>
     * To be noticed, relative returned NOT start with "/" and file separator will be replaced by "/" 
     * @param parent
     * @param child
     * @return
     * @throws IOException
     */
    public static final String relativePath(File parent, File child) throws IOException {
        String parentPath = parent.getCanonicalPath();
        String childPath = child.getCanonicalPath();

        if (parentPath.equalsIgnoreCase(childPath)) {
            return "";
        } else {
            return childPath.substring(parentPath.length() + 1).replace(File.separator, "/");
        }
    }
}

Related

  1. relativePath(File file, String path)
  2. relativePath(File from, File to)
  3. relativePath(File home, File f)
  4. relativePath(File IncludedFile, File UpperDirectory)
  5. relativePath(File parent, File child)
  6. relativePath(File path, File relativeTo)
  7. relativePath(File root, File node)
  8. relativePath(File src, File dest)
  9. relativePath(final File root, final File file)