Java Delete Folder deleteFolder(File folder)

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

Description

This method recursively deletes a folder.

License

Apache License

Parameter

Parameter Description
folder The root of the folder hierarchy to be deleted.

Declaration

public static void deleteFolder(File folder) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {
    /**/*from  ww w .j  a v  a2s  . c o  m*/
     * This method recursively deletes a folder. This is intended to delete
     * temporary folders used in tests. This method prints a message to stdout
     * if it is not able to delete the given folder. Typically this indicates
     * that a file in the hierarchy is still locked, probably because a stream
     * on it was not closed.
     * 
     * @param folder
     *            The root of the folder hierarchy to be deleted.
     */
    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                deleteFolder(file);
            }
            file.delete();
        }
        if (!folder.delete()) {
            System.out.println("Folder not deleted: " + folder.getPath());
        }
    }
}

Related

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