Here you can find the source of deleteFolder(File folder)
Parameter | Description |
---|---|
folder | the folder |
Parameter | Description |
---|---|
IOException | if can't delete |
public static void deleteFolder(File folder) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2008-2011 Chair for Applied Software Engineering, * Technische Universitaet Muenchen./*from www. j a v a2 s . c o m*/ * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: ******************************************************************************/ import java.io.File; import java.io.IOException; public class Main { /** * This method deletes a folder including all subfolders and files. * * @param folder the folder * @throws IOException if can't delete */ public static void deleteFolder(File folder) throws IOException { if (folder.exists()) { for (File child : folder.listFiles()) { if (child.isDirectory()) { deleteFolder(child); } else { if (!child.delete()) { throw new IOException("Deletion of file: " + child.getAbsolutePath() + " failed."); } } } if (!folder.delete()) { throw new IOException("Deletion of folder: " + folder.getAbsolutePath() + " failed."); } } } }