Created
May 23, 2014 20:09
-
-
Save xexes/31da682ef906489b54e3 to your computer and use it in GitHub Desktop.
dir deleter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.file.FileVisitResult; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.SimpleFileVisitor; | |
import java.nio.file.attribute.BasicFileAttributes; | |
public class DeleteFilesExample | |
{ | |
public static void main(String[] args) | |
{ | |
Path dir = Paths.get("c:/temp/innerDir"); | |
try | |
{ | |
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() | |
{ | |
@Override | |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) | |
throws IOException | |
{ | |
System.out.println("Deleting file: " + file); | |
Files.delete(file); | |
return FileVisitResult.CONTINUE; | |
} | |
@Override | |
public FileVisitResult postVisitDirectory(Path dir, | |
IOException exc) throws IOException | |
{ | |
System.out.println("Deleting dir: " + dir); | |
if (exc == null) { | |
Files.delete(dir); | |
return FileVisitResult.CONTINUE; | |
} else { | |
throw exc; | |
} | |
} | |
}); | |
} | |
catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment