Here you can find the source of deleteDirWithFiles(File dir, int maxDepth)
public static boolean deleteDirWithFiles(File dir, int maxDepth)
//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; } }