Skip to content

Instantly share code, notes, and snippets.

@xexes
Created May 23, 2014 20:09
Show Gist options
  • Save xexes/31da682ef906489b54e3 to your computer and use it in GitHub Desktop.
Save xexes/31da682ef906489b54e3 to your computer and use it in GitHub Desktop.
dir deleter
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