Java Folder Delete clearFiles(File dir)

Here you can find the source of clearFiles(File dir)

Description

Clears a directory of all its files.

License

Open Source License

Declaration

public static void clearFiles(File dir) throws IOException 

Method Source Code

//package com.java2s;
/**// w w  w  . j  a v  a 2 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.*;

public class Main {
    /**
     * Clears a directory of all its files. Will fail miserably
     * if any of them are actually directories
     */
    public static void clearFiles(File dir) throws IOException {
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++)
            files[i].delete();
    }

    /**
     * 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;
    }
}

Related

  1. clearDir(File d)
  2. clearDir(File dir)
  3. clearDir(File dir)
  4. clearDir(File dir)
  5. clearDir(File dir)
  6. clearFilesOnPath(String path)
  7. clearFolder(File f)
  8. clearFolder(File[] children)
  9. clearFolder(String directoryLocation)