Here you can find the source of getRelativePath(String base, String relative, boolean isBaseFile)
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. |
public static String getRelativePath(String base, String relative, boolean isBaseFile)
//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 © 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("/opt/some/path", "relative/path", false); * * // will print: /opt/some/relative/path * System.out.println("/opt/some/file.txt", "relative/path", 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; } }