Here you can find the source of deleteFolder(String folderPath)
public static void deleteFolder(String folderPath)
//package com.java2s; /*/*from w w w. ja v a 2 s .com*/ * This 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.File; public class Main { public static void deleteFolder(String folderPath) { deleteFolder(folderPath, false); } /** * Deletes a folder's contents * * @param folderPath the path to the folder to delete * @param keepRootFolder if true, only the contents will be deleted, false will delete the folder itself as well */ public static void deleteFolder(String folderPath, Boolean keepRootFolder) { File folderPathFiles = new File(folderPath); deleteFolder(folderPathFiles, keepRootFolder); } /** * Deletes a folder's contents * * @param folder the java.io.File folder to delete * @param keepRootFolder if true, only the contents will be deleted, false will delete the folder itself as well */ public static void deleteFolder(File folder, Boolean keepRootFolder) { if (!folder.isDirectory()) { return; } File[] files = folder.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { deleteFolder(files[i], false); } else { files[i].delete(); } } if (!keepRootFolder) { folder.delete(); } } }