Java Path Relative Get getRelativePath(String base, String relative, boolean isBaseFile)

Here you can find the source of getRelativePath(String base, String relative, boolean isBaseFile)

Description

Returns the path corresponding to the given base path, and the given relative path - results in "concatenation" of both, sort of.

License

Open Source License

Parameter

Parameter Description
base a base path.
relative the path to evaluate relatively to the given path.
isBaseFile if <code>true</code>, indicates that the base path corresponds to a file.

Return

the evaluated path.

Declaration

public static String getRelativePath(String base, String relative, boolean isBaseFile) 

Method Source Code

//package com.java2s;
/**//from  w  w w .j a  v  a 2  s  . c  o  m
 * This class holds various utility methods.
 * 
 * @author Yanick Duchesne
 *         <dl>
 *         <dt><b>Copyright: </b>
 *         <dd>Copyright &#169; 2002-2003 <a
 *         href="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
 *         Rights Reserved.</dd>
 *         </dt>
 *         <dt><b>License: </b>
 *         <dd>Read the license.txt file of the jar or visit the <a
 *         href="http://www.sapia-oss.org/license.html">license page </a> at the
 *         Sapia OSS web site</dd>
 *         </dt>
 *         </dl>
 */

import java.io.File;

public class Main {
    /**
     * Returns the path corresponding to the given base path, and the given
     * relative path - results in "concatenation" of both, sort of.
     * 
     * <pre>
     * 
     * // will print: /opt/some/path/relative/path
     * System.out.println(&quot;/opt/some/path&quot;, &quot;relative/path&quot;, false);
     * 
     * // will print: /opt/some/relative/path
     * System.out.println(&quot;/opt/some/file.txt&quot;, &quot;relative/path&quot;, true);
     * </pre>
     * 
     * @param base
     *          a base path.
     * @param relative
     *          the path to evaluate relatively to the given path.
     * @param isBaseFile
     *          if <code>true</code>, indicates that the base path corresponds
     *          to a file.
     * @return the evaluated path.
     */
    public static String getRelativePath(String base, String relative, boolean isBaseFile) {
        String toReturn;
        String compared = base.replace('\\', '/');

        if (isBaseFile) {
            int idx;

            if ((idx = compared.lastIndexOf('/')) >= 0) {
                toReturn = base.substring(0, idx) + File.separator + relative;
            } else {
                toReturn = base + File.separator + relative;
            }
        } else {
            if (compared.endsWith("//")) {
                toReturn = base + relative;
            } else {
                toReturn = base + File.separator + relative;
            }
        }

        return toReturn;
    }
}

Related

  1. getRelativePath(final File fromFile, final File toFile)
  2. getRelativePath(final File parentDir, final File file)
  3. getRelativePath(final File parentDirectory, final File file)
  4. getRelativePath(final String base, final File targetFile)
  5. getRelativePath(String base, String fullPath)
  6. getRelativePath(String baseFullPath, File file)
  7. getRelativePath(String basePath, String path)
  8. getRelativePath(String basePath, String pathToRelativize)
  9. getRelativePath(String fileName, String projFile)