/** * Gets the <code>String</code> representing the path to a file relative to * a given directory. * @param file The <code>File</code> for which to obtain the relative path. * @param base The <code>File</code> representing the directory that the * resulting path should be relative to. * @return The <code>String</code> representing the relative path. * @throws IOException If a directory along the walk from <code>base</code> * to <code>file</code> could not be read. */ public static String getRelativePath(File file, File base) throws IOException { StringWriter path = new StringWriter(); while (!isAncestor(file, base)) { path.append("../"); } String fileName = file.getAbsolutePath(); String baseName = base.getAbsolutePath(); int prefixLength = baseName.length(); if (!baseName.endsWith("/")) { prefixLength++; } path.append(fileName.substring(prefixLength)); return path.toString(); }