public static String relativize ( final String path , final File parentDir , final boolean removeInitialFileSep ) { // Check sanity Validate . notNull ( path , "path" ) ; Validate . notNull ( parentDir , "parentDir" ) ; final String basedirPath = FileSystemUtilities . getCanonicalPath ( parentDir ) ; String toReturn = path ; // Compare case insensitive if ( path . toLowerCase ( ) . startsWith ( basedirPath . toLowerCase ( ) ) ) { toReturn = path . substring ( basedirPath . length ( ) ) ; } // Handle whitespace in the argument. return removeInitialFileSep && toReturn . startsWith ( File . separator ) ? toReturn . substring ( File . separator . length ( ) ) : toReturn ; }
If the supplied path refers to a file or directory below the supplied basedir the returned path is identical to the part below the basedir .