/** * Walks a directory tree using post-order traversal. The contents of a * directory are visited before the directory itself is visited. * @param root The <code>File</code> indicating the file or directory to * walk. * @param visitor The <code>FileVisitor</code> to use to visit files and * directories while walking the tree. * @return A value indicating whether the tree walk was completed without * {@link FileVisitor#visit(File)} ever returning false. * @throws Exception If {@link FileVisitor#visit(File)} threw an exception. * @see FileVisitor#visit(File) */ public static boolean postOrderTraversal(File root, FileVisitor visitor) throws Exception { if (root.isDirectory()) { for (File child : root.listFiles()) { if (!postOrderTraversal(child, visitor)) { return false; } } } return visitor.visit(root); }
/** * Walks a directory tree using pre-order traversal. The contents of a * directory are visited after the directory itself is visited. * @param root The <code>File</code> indicating the file or directory to * walk. * @param visitor The <code>FileVisitor</code> to use to visit files and * directories while walking the tree. * @return A value indicating whether the tree walk was completed without * {@link FileVisitor#visit(File)} ever returning false. * @throws Exception If {@link FileVisitor#visit(File)} threw an exception. * @see FileVisitor#visit(File) */ public static boolean preOrderTraversal(File root, FileVisitor visitor) throws Exception { if (!visitor.visit(root)) { return false; } if (root.isDirectory()) { for (File child : root.listFiles()) { if (!preOrderTraversal(child, visitor)) { return false; } } } return true; }