Here you can find the source of relativePath(File descendant, File root)
Parameter | Description |
---|---|
descendant | the descendant file |
root | the root folder |
public static String relativePath(File descendant, File root)
//package com.java2s; //License from project: Apache License import java.io.*; public class Main { /**/* www . j a v a2 s . co m*/ * Compute the relative path between two files: * root descendant returned * /a/b/c /a/b/c/d.txt d.txt * /a/b/c /a/b/c/d/e.txt d/e.txt * /a/b/c /a/b/d.txt '' * * @param descendant the descendant file * @param root the root folder * * @return the relative path if files match, empty string otherwise */ public static String relativePath(File descendant, File root) { try { String rootPath = root.getCanonicalPath().replace('\\', '/'); String descendantPath = descendant.getCanonicalPath().replace('\\', '/'); if (descendantPath.startsWith(rootPath)) return descendantPath.substring(rootPath.length() + 1); return ""; } catch (IOException ex) { return ""; } } }