Java Delete Tree deltree(File directory)

Here you can find the source of deltree(File directory)

Description

Deletes the given file and everything under it.

License

BSD License

Return

Whether all files were deleted succesfully.

Declaration

public static boolean deltree(File directory) 

Method Source Code


//package com.java2s;
/*/* www  . j  a v  a 2s  . com*/
 * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
 *
 * Licensed under the Aduna BSD-style license.
 */

import java.io.File;

public class Main {
    /**
     * Deletes the given file and everything under it.
     * 
     * @return Whether all files were deleted succesfully.
     */
    public static boolean deltree(File directory) {
        if (directory == null || !directory.exists()) {
            return true;
        }

        boolean result = true;
        if (directory.isFile()) {
            result = directory.delete();
        } else {
            File[] list = directory.listFiles();
            for (int i = list.length; i-- > 0;) {
                if (!deltree(list[i])) {
                    result = false;
                }
            }
            if (!directory.delete()) {
                result = false;
            }
        }
        return result;
    }
}

Related

  1. deleteTree(final File file)
  2. delTree(File dir)
  3. delTree(File dir)
  4. delTree(File dir, boolean deleteDirItSelf)
  5. delTree(File directory)
  6. delTree(File f)
  7. delTree(File file)
  8. delTree(File file)
  9. delTree(File file)