Java Directory Delete nio deleteDirectory(Path dirToDelete)

Here you can find the source of deleteDirectory(Path dirToDelete)

Description

delete Directory

License

Open Source License

Declaration

public static void deleteDirectory(Path dirToDelete) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  ww.  j a va2  s  .c  o  m*/
 * Copyright (C) 2014 Shashank Tulsyan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import static java.nio.file.FileVisitResult.CONTINUE;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

public class Main {
    public static void deleteDirectory(Path dirToDelete) throws IOException {
        if (!Files.isDirectory(dirToDelete))
            throw new IllegalArgumentException("the path is not a directory");
        Files.walkFileTree(dirToDelete, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("Deleting file: " + file);
                Files.delete(file);
                return CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("Deleting dir: " + dir);
                if (exc == null) {
                    try {
                        Files.delete(dir);
                    } catch (Exception a) {
                        listDirectory(dir);
                    }
                    return CONTINUE;
                } else {
                    throw exc;
                }
            }

            private int listDirectory(Path dir) {
                int cnt = 0;
                try {
                    System.out.println("+++dir+++");
                    try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir)) {
                        for (Path path : ds) {
                            System.out.println(path);
                            cnt++;
                        }
                    }
                    System.out.println("---dir---");
                } catch (Exception a) {
                }
                return cnt;
            }
        });
    }
}

Related

  1. deleteDirectory(Path dir)
  2. deleteDirectory(Path directory)
  3. deleteDirectory(Path directory)
  4. deleteDirectory(Path directory)
  5. deleteDirectory(Path directory)
  6. deleteDirectory(Path path)
  7. deleteDirectory(String path)
  8. deleteDirectoryAndContents(String dir)
  9. deleteDirectoryRecursively(Path dir)