Java Path Relative Get getRelativePath(final File base, final File name)

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

Description

get Relative Path

License

Open Source License

Declaration

public static String getRelativePath(final File base, final File name) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w  w  w  .  j  av  a  2s.  c o m*/
 *  Copyright (C) 2006-2015  Ronald Blankendaal
 *
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

import java.io.File;

import java.io.IOException;

public class Main {
    public static String getRelativePath(final File base, final File name) throws IOException {
        File parent = base.getParentFile();
        if (parent == null)
            return name.getPath();

        String bpath = base.getCanonicalPath();
        String fpath = name.getCanonicalPath();

        if (fpath.startsWith(bpath)) {
            return fpath.substring(bpath.length() + 1);
        } else {
            return ".." + File.separatorChar + getRelativePath(parent, name);
        }
    }
}

Related

  1. getRelativePath(File subj, File relativeTo)
  2. getRelativePath(File target, File base)
  3. getRelativePath(File target, File relativeTo)
  4. getRelativePath(File wd, File file)
  5. getRelativePath(final File base, final File child)
  6. getRelativePath(final File baseDirectory, final File f)
  7. getRelativePath(final File basePathFile, final File pathFile)
  8. getRelativePath(final File file, final File folder)
  9. getRelativePath(final File from, final File to)