Java Path Relative Get getRelativePath(File fromFolder, File toFile)

Here you can find the source of getRelativePath(File fromFolder, File toFile)

Description

Gets the relative path of a file wrt one of its parent directories.

License

Apache License

Parameter

Parameter Description
fromFolder The parent directory
toFile The file

Exception

Parameter Description
IOException On error, for instance if toFile is not in fromFolder

Return

String The relative path

Declaration


public static String getRelativePath(File fromFolder, File toFile) throws IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w. j a  v  a2 s  .  c  om*/
 * Copyright (C) 2000-2010 Atomikos <info@atomikos.com>
 *
 * This code ("Atomikos TransactionsEssentials"), by itself,
 * is being distributed under the
 * Apache License, Version 2.0 ("License"), a copy of which may be found at
 * http://www.atomikos.com/licenses/apache-license-2.0.txt .
 * You may not use this file except in compliance with the License.
 *
 * While the License grants certain patent license rights,
 * those patent license rights only extend to the use of
 * Atomikos TransactionsEssentials by itself.
 *
 * This code (Atomikos TransactionsEssentials) contains certain interfaces
 * in package (namespace) com.atomikos.icatch
 * (including com.atomikos.icatch.Participant) which, if implemented, may
 * infringe one or more patents held by Atomikos.
 * It should be appreciated that you may NOT implement such interfaces;
 * licensing to implement these interfaces must be obtained separately from Atomikos.
 *
 * 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.
 */

import java.io.File;
import java.io.IOException;

public class Main {
    /**
     * Gets the relative path of a file wrt one of its parent directories.
     *
     * @param fromFolder The parent directory
     * @param toFile The file
     * @return String The relative path
     * @throws IOException On error, for instance if toFile is not in fromFolder
     */

    public static String getRelativePath(File fromFolder, File toFile) throws IOException {

        String basePath = fromFolder.getAbsolutePath();
        String filePath = toFile.getAbsolutePath();
        if (!filePath.startsWith(basePath))
            throw new IOException("The specified file is not within the folder.");
        return filePath.substring(basePath.length() + 1);

    }
}

Related

  1. getRelativePath(File fromDir, File toFile)
  2. getRelativePath(File fromFile, File toFile)
  3. getRelativePath(File fromFile, File toFile)
  4. getRelativePath(File fromFile, File toFile)
  5. getRelativePath(File fromFile, File toFile)
  6. getRelativePath(File home, File f)
  7. getRelativePath(File home, File f)
  8. getRelativePath(File home, File f)
  9. getRelativePath(File original, File directory)