Here you can find the source of getRelativePathToBase(File path, File basePath)
Parameter | Description |
---|---|
path | a path |
basePath | the base path |
public static String getRelativePathToBase(File path, File basePath)
//package com.java2s; // are made available under the terms of the Eclipse Public License v1.0 import java.io.File; public class Main { /**//from w w w . ja v a 2 s .c om * Returns the relative path of a path from a base path. * * @param path * a path * @param basePath * the base path * @return a relative path */ public static String getRelativePathToBase(File path, File basePath) { try { String dir = path.toURL().toExternalForm(); String baseDir = basePath.toURL().toExternalForm(); StringBuffer result = new StringBuffer(); if (dir.indexOf(baseDir) == 0) { String delta = dir.substring(baseDir.length()); for (int i = 0; i < delta.length(); i++) { if (delta.charAt(i) == '/') { result.append("../"); //$NON-NLS-1$ } } } return result.toString(); } catch (Exception e) { return ""; //$NON-NLS-1$ } } }