Java File Path Delete deleteDirectory(File path)

Here you can find the source of deleteDirectory(File path)

Description

Deletes a directory and its files recursively.

License

Open Source License

Parameter

Parameter Description
path the path

Return

true, if successful

Declaration

public static boolean deleteDirectory(File path) 

Method Source Code

//package com.java2s;
/*/*from   w  w w  . j a  v  a  2  s . c  o m*/
 * (c) 2012 Michael Schwartz e.U.
 * All Rights Reserved.
 * 
 * This program is not a free software. The owner of the copyright
 * can license the software for you. You may not use this file except in
 * compliance with the License. In case of questions please
 * do not hesitate to contact us at idx@mschwartz.eu.
 * 
 * Filename: FileUtil.java
 * Created: 30.05.2012
 * 
 * Author: $LastChangedBy$
 * Date: $LastChangedDate$
 * Revision: $LastChangedRevision$
 */

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Deletes a directory and its files recursively.
     * 
     * @param path
     *            the path
     * @return true, if successful
     */
    public static boolean deleteDirectory(File path) {
        File[] files = path.listFiles();
        for (File file : files) {
            try {
                if (file.isDirectory()) {
                    // System.out.println("  delete dir " +
                    // file.getCanonicalPath());
                    boolean ok = deleteDirectory(file);
                    if (!ok) {
                        System.out.println("  ups, " + file.getCanonicalPath() + " cannot be deleted");
                    }
                } else {
                    // System.out.println("  delete file " +
                    // file.getCanonicalPath());
                    boolean ok = file.delete();
                    if (!ok) {
                        System.out.println("  ups, " + file.getCanonicalPath() + " cannot be deleted");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return (path.delete());
    }

    /**
     * List the elements of the directory and its sub-directories.
     * 
     * Return a list of the files
     * 
     */
    public static List<File> listFiles(File directory) {
        List<File> l = new ArrayList<File>();
        File listOfFiles[] = directory.listFiles();
        if (listOfFiles != null) {
            for (File f : listOfFiles) {
                if (f.isDirectory()) {
                    l.addAll(listFiles(f));
                } else {
                    l.add(f);
                }
            }
        }
        return l;
    }
}

Related

  1. deleteDirectory(File path)
  2. deleteDirectory(File path)
  3. deleteDirectory(File path)
  4. deleteDirectory(File path)
  5. deleteDirectory(File path)
  6. deleteDirectory(File path)
  7. deleteDirectory(File path)
  8. deleteDirectory(File path)
  9. deleteDirectory(File path)