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

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

Description

returns the relative path of file to base.

License

Open Source License

Exception

Parameter Description
IOException if position of file is not relative to base

Declaration

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

Method Source Code

//package com.java2s;
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/

import java.io.File;

import java.io.IOException;

public class Main {
    /** returns the relative path of file to base.
     * @throws IOException if position of file is not relative  to base
    *///from  w ww.j a va 2s. c om
    public static String getRelativePath(File base, File file) throws IOException {
        String filePath = file.getAbsoluteFile().getCanonicalPath();
        String basePath = base.getAbsoluteFile().getCanonicalPath();
        int start = filePath.indexOf(basePath);
        if (start != 0)
            throw new IOException(basePath + " not ancestor of " + filePath);
        return filePath.substring(basePath.length());
    }
}

Related

  1. getRelativePath(@Nullable File projectFile, @Nullable File rootFile)
  2. getRelativePath(File a, File b, String pathSep)
  3. getRelativePath(File ancestor, File file)
  4. getRelativePath(File base, File absoluteFile)
  5. getRelativePath(File base, File file)
  6. getRelativePath(File base, File file)
  7. getRelativePath(File base, File file)
  8. getRelativePath(File base, File name)
  9. getRelativePath(File base, File name)