Here you can find the source of deleteFileOrDir(File dir, int max)
static void deleteFileOrDir(File dir, int max)
//package com.java2s; import java.io.File; public class Main { static void deleteFileOrDir(File dir, int max) { File[] listFiles = dir.listFiles(); if (listFiles != null && max > 0) { for (File file : listFiles) { deleteFileOrDir(file, max - 1); }/*w w w . j a v a 2 s .com*/ } dir.delete(); } }