Java Delete Directory deleteFiles(File directory)

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

Description

Deletes all files and directories in the specified directory.

License

BSD License

Return

true when all files in the specified directory were successfully deleted, when there where no files or when the specified file was not a directory.

Declaration

public static boolean deleteFiles(File directory) 

Method Source Code


//package com.java2s;
/*/* w w w .j a  v  a  2  s.c o m*/
 * 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 all files and directories in the specified directory. Nothing
     * happens when the specified File is not a directory.
     * 
     * @return true when all files in the specified directory were successfully
     *         deleted, when there where no files or when the specified file was
     *         not a directory.
     */
    public static boolean deleteFiles(File directory) {
        boolean result = true;

        if (directory.isDirectory()) {
            File[] list = directory.listFiles();
            for (int i = list.length; i-- > 0;) {
                File file = list[i];
                if (file.isFile()) {
                    result = result && file.delete();
                }
            }
        }

        return result;
    }
}

Related

  1. deleteDirectoryContents(final File dir)
  2. deleteDirectoryContents(final File dir, int deleteDirLevel, int level)
  3. deleteFiles(File dir)
  4. deleteFiles(File dir)
  5. deleteFiles(File directory)
  6. deleteFiles(File directory)
  7. deleteFiles(File directory, String prefix)
  8. deleteFiles(File docFolder)
  9. deleteFiles(File f, boolean del_self)