Here you can find the source of getRelativePath(String path, String base)
Parameter | Description |
---|---|
path | a parameter |
base | a parameter |
public static String getRelativePath(String path, String base)
//package com.java2s; /*/*ww w. j a va2 s .c om*/ * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. * * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser 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.util.StringTokenizer; public class Main { /** * get relative path according the give path and base * * @param path * @param base * @return relative path */ public static String getRelativePath(String path, String base) { if (path == null || base == null) return null; // On Windows Plaform, change all the path string to lower case if (System.getProperty("os.name").toLowerCase() .startsWith("windows")) { path = path.toLowerCase(); base = base.toLowerCase(); } if (path.lastIndexOf(base) < 0) { int index = path.lastIndexOf("/"); if (index < 0) { return path; } else { return path.substring(index + 1); } } String relPath = path.substring(path.lastIndexOf(base) + base.length()); StringTokenizer st = new StringTokenizer(relPath, "/", false); String nativeRelPath = ""; while (st.hasMoreTokens()) { if (nativeRelPath.length() == 0) { nativeRelPath = st.nextToken(); } else { nativeRelPath += File.separator + st.nextToken(); } } return nativeRelPath; } }