Adam Bien's Weblog

Tuesday Feb 25, 2014

Java 7+: Deleting Recursively A Directory

Path directory = Paths.get("/tmp");
   Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
	   public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
		   return FileVisitResult.CONTINUE;

	   public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
		   return FileVisitResult.CONTINUE;


Special Events: Java 8 with Java EE 7: "More Power with Less Code", 13th October, 2014 and Java EE 7: "Testing and Code Quality", 14th October, 2014

A book about rethinking Java EE Patterns


Is this pro or con Java 7+? This task should be a one liner. Same as reading the file content to a String. I have no idea why this is still not possible in plain Java SE.

Posted by Franz van Betteraey on February 25, 2014 at 08:34 AM CET #

How about this one liner:
FileUtils.deleteDirectory(path.toFile()); ?
Commons IO does a good job here, otherwhise you will have to encapsulate that code using FileVisitor.

Posted by Nestor on March 01, 2014 at 01:27 AM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments