Java File Delete delete(File file)

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

Description

Deletes a file or directory recursively.

License

Open Source License

Declaration

public static boolean delete(File file) 

Method Source Code

//package com.java2s;
/**/* w w  w.  j  ava2  s.  c o m*/
 * AC - A source-code copy detector
 *
 *     For more information please visit:  http://github.com/manuel-freire/ac
 *
 * ****************************************************************************
 *
 * This file is part of AC, version 2.0
 *
 * AC is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * AC 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with AC.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

import java.util.ArrayList;

public class Main {
    /**
     * Deletes a file or directory recursively. Returns 'true' if ok, or
     * 'false' on error.
     */
    public static boolean delete(File file) {
        // Recursive call
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (!delete(files[i]))
                    return false;
            }
        }
        // File deletion
        if (!file.delete()) {
            System.err.println("Imposible to delete file "
                    + file.getAbsolutePath());
            return false;
        }
        return true;
    }

    /**
     * Lists all files in a given location (recursive)
     */
    public static ArrayList<File> listFiles(File dir) {
        ArrayList<File> al = new ArrayList<File>();
        for (File f : dir.listFiles()) {
            if (f.isDirectory()) {
                al.addAll(listFiles(f));
            } else {
                al.add(f);
            }
        }
        return al;
    }
}

Related

  1. delete(File file)
  2. delete(File file)
  3. delete(File file)
  4. delete(File file)
  5. delete(File file)
  6. delete(File file)
  7. delete(File file)
  8. delete(File file)
  9. delete(File file)