Java Delete Tree delTree(File file)

Here you can find the source of delTree(File file)

Description

Delete a directory recursively or delete a single file

License

Open Source License

Parameter

Parameter Description
file to delete

Return

true if sucessfully deleted

Declaration

public static boolean delTree(File file) 

Method Source Code

//package com.java2s;
/*//from  ww w  .  j  a v a  2s  . c  o  m
 *  Gruntspud
 *
 *  Copyright (C) 2002 Brett Smith.
 *
 *  Written by: Brett Smith <t_magicthize@users.sourceforge.net>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

import java.io.File;

public class Main {
    /**
     * Delete a directory recursively or delete a single file
     *  
     * @param file to delete
     * @return true if sucessfully deleted
     */
    public static boolean delTree(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; files != null && i < files.length; i++) {
                    if (!delTree(files[i])) {
                        return false;
                    }
                }
            }
            if (!file.delete()) {
                return false;
            } else {
                if (file.exists()) {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. delTree(File file)
  2. delTree(File file)
  3. delTree(File file)
  4. delTree(File file)
  5. delTree(File file)
  6. delTree(File root)
  7. deltree(File root)
  8. deltree(final String directory)
  9. deltree(String dir)