code: public static boolean isAncestor(File file, File ancestor) throws IOException {
      file = file.getCanonicalFile();
      ancestor = ancestor.getCanonicalFile();
      do {
        if (file.equals(ancestor)) {
          return true;
        }
        file = file.getParentFile();
      } while (file != null);
      return false;
    }
docs: Determines if the specified directory is an ancestor of the specified
  file or directory.
  @param file The file or directory to test.
  @param ancestor The directory for which to determine whether
  <code>file</code> is an ancestor.
  @return <code>true</code> if <code>ancestor</code> is equal to or an
  ancestor of <code>file</code>, <code>false</code> otherwise.
  @throws IOException if an error occurs in attempting to canonicalize
  {@code file} or {@code ancestor}.