Here you can find the source of deleteDirectory(String fileName)
public static void deleteDirectory(String fileName)
//package com.java2s; // Use of this software is governed by the GPLv3 license import java.io.File; import java.io.IOException; public class Main { public static void deleteDirectory(String fileName) { File directory = new File(fileName); //make sure directory exists if (!directory.exists()) { //do nothing } else {/*from ww w.j a va2 s .co m*/ try { deleteResursive(directory); } catch (IOException e) { e.printStackTrace(); } } } public static void deleteResursive(File file) throws IOException { if (file.isDirectory()) { //directory is empty, then delete it if (file.list().length == 0) { file.delete(); } else { //list all the directory contents String files[] = file.list(); for (String temp : files) { //construct the file structure File fileDelete = new File(file, temp); //recursive delete deleteResursive(fileDelete); } //check the directory again, if empty then delete it if (file.list().length == 0) { file.delete(); } } } else { //if file, then delete it file.delete(); } } }