使用SimpleFileVisitor类删除目录
import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.Files;//来 自 时 代 J a v a - nowjava.com import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; public class Main { public static void main(String[] args) { try { Files.walkFileTree(Paths.get("/home"), new DeleteDirectory()); } catch (IOException ex) { ex.printStackTrace(); } } }/**来 自 nowjava.com - 时代Java**/ class DeleteDirectory extends SimpleFileVisitor<Path> { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException { System.out.println("Deleting " + file.getFileName()); Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path directory, IOException exception) throws IOException { if (exceptio