Java Delete Folder deleteFolder(File folder)

Here you can find the source of deleteFolder(File folder)

Description

This method deletes a folder including all subfolders and files.

License

Open Source License

Parameter

Parameter Description
folder the folder

Exception

Parameter Description
IOException if can't delete

Declaration

public static void deleteFolder(File folder) throws IOException 

Method Source Code

//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.");
            }
        }
    }
}

Related

  1. deleteFolder(File file)
  2. deleteFolder(File file)
  3. deleteFolder(File file, boolean deleteParent)
  4. deleteFolder(File file, boolean withCurrentFolder)
  5. deleteFolder(File folder)
  6. deleteFolder(File folder)
  7. deleteFolder(File folder)
  8. deleteFolder(File folder)
  9. deleteFolder(File folder)