Java Relative Path Get relativePath(File root, File node)

Here you can find the source of relativePath(File root, File node)

Description

Computes the path name of a file node relative to a given root node.

License

Apache License

Parameter

Parameter Description
root the parent node
node the file node to compute the relative path for

Exception

Parameter Description
IOException when an I/O error occurs during resolving the canonical path of the files

Return

the path of node relative to root

Declaration

public static String relativePath(File root, File node) throws IOException 

Method Source Code

//package com.java2s;
/**/*ww w. j av a2s.  com*/
 *    Copyright 2013 Thomas Rausch
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Computes the path name of a file node relative to a given root node.
     * <p/>
     * If the root is {@code /home/cdlflex/custom-ahy} and the given node is
     * {@code /home/cdlflex/custom-ahy/assembly/pom.xml}, the returned path name will be {@code assembly/pom.xml}.
     * 
     * @param root the parent node
     * @param node the file node to compute the relative path for
     * @return the path of {@code node} relative to {@code root}
     * @throws IOException when an I/O error occurs during resolving the canonical path of the files
     */
    public static String relativePath(File root, File node) throws IOException {
        String rootPath = root.getCanonicalPath();
        String nodePath = node.getCanonicalPath();

        return nodePath.substring(rootPath.length() + 1);
    }
}

Related

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