Java Relative Path Get relativize(File path, File relative)

Here you can find the source of relativize(File path, File relative)

Description

relativize

License

Open Source License

Declaration

public static File relativize(File path, File relative) throws IOException 

Method Source Code


//package com.java2s;
/* ----------------------------------------------------------------------
 * /*from   w w w.j a  va2 s . com*/
 * Copyright (c) 2002-2009 The MITRE Corporation
 * 
 * Except as permitted below
 * ALL RIGHTS RESERVED
 * 
 * The MITRE Corporation (MITRE) provides this software to you without
 * charge to use for your internal purposes only. Any copy you make for
 * such purposes is authorized provided you reproduce MITRE's copyright
 * designation and this License in any such copy. You may not give or
 * sell this software to any other party without the prior written
 * permission of the MITRE Corporation.
 * 
 * The government of the United States of America may make unrestricted
 * use of this software.
 * 
 * This software is the copyright work of MITRE. No ownership or other
 * proprietary interest in this software is granted you other than what
 * is granted in this license.
 * 
 * Any modification or enhancement of this software must inherit this
 * license, including its warranty disclaimers. You hereby agree to
 * provide to MITRE, at no charge, a copy of any such modification or
 * enhancement without limitation.
 * 
 * MITRE IS PROVIDING THE PRODUCT "AS IS" AND MAKES NO WARRANTY, EXPRESS
 * OR IMPLIED, AS TO THE ACCURACY, CAPABILITY, EFFICIENCY,
 * MERCHANTABILITY, OR FUNCTIONING OF THIS SOFTWARE AND DOCUMENTATION. IN
 * NO EVENT WILL MITRE BE LIABLE FOR ANY GENERAL, CONSEQUENTIAL,
 * INDIRECT, INCIDENTAL, EXEMPLARY OR SPECIAL DAMAGES, EVEN IF MITRE HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You accept this software on the condition that you indemnify and hold
 * harmless MITRE, its Board of Trustees, officers, agents, and
 * employees, from any and all liability or damages to third parties,
 * including attorneys' fees, court costs, and other related costs and
 * expenses, arising out of your use of this software irrespective of the
 * cause of said liability.
 * 
 * The export from the United States or the subsequent reexport of this
 * software is subject to compliance with United States export control
 * and munitions control restrictions. You agree that in the event you
 * seek to export this software you assume full responsibility for
 * obtaining all necessary export licenses and approvals and for assuring
 * compliance with applicable reexport restrictions.
 * 
 * ----------------------------------------------------------------------
 * 
 * NOTICE
 * 
 * This software was produced for the U. S. Government
 * under Contract No. W15P7T-09-C-F600, and is
 * subject to the Rights in Noncommercial Computer Software
 * and Noncommercial Computer Software Documentation
 * Clause 252.227-7014 (JUN 1995).
 * 
 * (c) 2009 The MITRE Corporation. All Rights Reserved.
 * 
 * ----------------------------------------------------------------------
 *
 */

import java.io.File;

import java.io.IOException;

public class Main {
    public static File relativize(File path, File relative) throws IOException {
        if (!relative.isDirectory()) {
            relative = relative.getParentFile();
        }
        String aPath = (path.isDirectory() ? path : path.getParentFile()).getCanonicalPath();
        String aRel = relative.getCanonicalPath();
        aPath = fixSeparatorChars(aPath);
        aRel = fixSeparatorChars(aRel);
        String[] aPaths = aPath.split("/");
        String[] aRels = aRel.split("/");
        String common = "";
        for (int i = 0; i < aPaths.length || i < aRels.length; i++) {
            if (i >= aPaths.length || i >= aRels.length || !aPaths[i].equals(aRels[i])) {
                for (int j = aRels.length; j > i; j--) {
                    common += "../";
                }
                while (i < aPaths.length) {
                    common += aPaths[i++] + '/';
                }
                break;
            }
        }
        String rel = common + path.getName();
        return new File(rel);
    }

    public static String fixSeparatorChars(String path) {
        if (File.separatorChar != '/') {
            path = path.replace(File.separatorChar, '/');
        }
        return path;
    }
}

Related

  1. relativeToAbsoluteFilePath(String s)
  2. relativize(File base, File absolute)
  3. relativize(File base, File child)
  4. relativize(File file, File basedir)
  5. relativize(File home, File f)
  6. relativize(final File root, final File target)
  7. relativize(String path)
  8. relativize(String path)
  9. relativize(String path, String base)