Here you can find the source of getRelativePath(File f, File base)
Parameter | Description |
---|---|
f | a parameter |
base | a parameter |
Parameter | Description |
---|---|
IllegalArgumentException | if f is not a sub path of base |
public static String getRelativePath(File f, File base) throws IllegalArgumentException
//package com.java2s; //License from project: Open Source License import java.io.File; public class Main { /**//from w w w .ja v a 2s . com * * @param f * @param base * @return the path of f, relative to base. e.g. "/a/b/c.txt" relative to * "/a" is "b/c.txt" This method uses absolute paths. * @throws IllegalArgumentException * if f is not a sub path of base * @testedby {@link FileUtilsTest#testGetRelativePath()} */ public static String getRelativePath(File f, File base) throws IllegalArgumentException { String fp = f.getAbsolutePath(); String bp = base.getAbsolutePath(); if (!fp.startsWith(bp)) { if (f.equals(base)) return ""; // Is this what we want? throw new IllegalArgumentException(f + "=" + fp + " is not a sub-file of " + base + "=" + bp); } String rp = fp.substring(bp.length()); if (rp.isEmpty()) { return rp; // f = base } char ec = rp.charAt(0); // TODO a bit more efficient if (ec == '\\' || ec == '/') { rp = rp.substring(1); } return rp; } }