public static String computeRelativePath ( File file , File relativeTo ) throws IOException { String [ ] realDirs = getCanonicalPathElements ( file ) ; String [ ] relativeToDirs = getCanonicalPathElements ( relativeTo ) ; // Eliminate the common root int common = 0 ; for ( ; common < realDirs . length && common < relativeToDirs . length ; common ++ ) { if ( ! realDirs [ common ] . equals ( relativeToDirs [ common ] ) ) { break ; } } String relativePath = "" ; // For each remaining level in the file path, add a .. for ( int ii = 0 ; ii < ( realDirs . length - common ) ; ii ++ ) { relativePath += ".." + File . separator ; } // For each level in the resource path, add the path for ( ; common < relativeToDirs . length ; common ++ ) { relativePath += relativeToDirs [ common ] + File . separator ; } return relativePath ; }
Computes a relative path between to Files