Java Directory Delete nio deleteDir(File dir)

Here you can find the source of deleteDir(File dir)

Description

delete Dir

License

Mozilla Public License

Declaration

public static void deleteDir(File dir) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Mozilla Public License 

import java.io.File;

import java.io.IOException;

import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    private static final SimpleFileVisitor<Path> nukeVisitor = new SimpleFileVisitor<Path>() {

        private FileVisitResult delete(Path file) throws IOException {
            Files.delete(file);//from w  w w . jav  a  2 s . com
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            return delete(file);
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            return delete(file);
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            return delete(dir);
        }

    };

    public static void deleteDir(File dir) throws IOException {
        Files.walkFileTree(dir.toPath(), nukeVisitor);
    }
}

Related

  1. deleteDir(File file)
  2. deleteDir(final File targetDir)
  3. deleteDir(String dirName)
  4. deleteDir(String path)