Java Directory Delete nio deleteDirWithFiles(File dir, int maxDepth)

Here you can find the source of deleteDirWithFiles(File dir, int maxDepth)

Description

delete Dir With Files

License

Apache License

Declaration

public static boolean deleteDirWithFiles(File dir, int maxDepth) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

import java.nio.file.Files;

import java.util.stream.Stream;

public class Main {
    public static boolean deleteDirWithFiles(File dir, int maxDepth) {
        File[] entries = dir.listFiles();
        if (entries == null)
            return false;
        Stream.of(entries).filter(File::isDirectory).forEach(f -> {
            if (maxDepth < 1) {
                throw new AssertionError("Contains directory " + f);
            } else {
                deleteDirWithFiles(f, maxDepth - 1);
            }//from   www  . j a v  a  2  s .c  o m
        });
        Stream.of(entries).forEach(f -> {
            try {
                Files.delete(f.toPath());
            } catch (IOException e) {
            }
        });
        if (dir.listFiles() == null)
            return dir.delete();
        return false;
    }
}

Related

  1. deleteDirectorySelectively(Path path, Predicate predicate)
  2. deleteDirIfEmpty(Path dir)
  3. deleteDirIfExists(Path dirPath)
  4. deleteDirOrFile(Path p, boolean followSymLinkDir)
  5. deleteDirReqursivelyIfExists(File dir)
  6. deleteFileOrDirectory(File file)
  7. deleteFileOrDirectory(final File source)
  8. deleteFileOrFolder(final Path path)