Java Delete Directory deleteDirectoryContent(final String strDir)

Here you can find the source of deleteDirectoryContent(final String strDir)

Description

Delete all files and directories in directory but do not delete the directory itself.

License

Open Source License

Parameter

Parameter Description
strDir - string that specifies directory to delete

Return

boolean - sucess flag

Declaration

public static boolean deleteDirectoryContent(final String strDir) 

Method Source Code

//package com.java2s;
/*//from w  w  w  . ja  v a  2s.  c  om
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library 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. 
 */

import java.io.File;

public class Main {
    /** 
     * Delete all files and directories in directory but do not delete the
     * directory itself.
     * 
     * @param strDir - string that specifies directory to delete
     * @return boolean - sucess flag
     */
    public static boolean deleteDirectoryContent(final String strDir) {
        return ((strDir != null) && (strDir.length() > 0)) ? deleteDirectoryContent(new File(strDir)) : false;
    }

    /** 
     * Delete all files and directories in directory but do not delete the
     * directory itself.
     * 
     * @param fDir - directory to delete
     * @return boolean - sucess flag
     */
    public static boolean deleteDirectoryContent(final File fDir) {
        boolean bRetval = false;

        if (fDir != null && fDir.isDirectory()) {
            final File[] files = fDir.listFiles();

            if (files != null) {
                bRetval = true;
                boolean dirDeleted;

                for (int index = 0; index < files.length; index++) {
                    if (files[index].isDirectory()) {
                        // TODO: Performance: Implement this as a queue where you add to
                        // the end and take from the beginning, it will be more efficient
                        // than the recursion
                        dirDeleted = deleteDirectoryContent(files[index]);
                        if (dirDeleted) {
                            bRetval = bRetval && files[index].delete();
                        } else {
                            bRetval = false;
                        }
                    } else {
                        bRetval = bRetval && files[index].delete();
                    }
                }
            }
        }

        return bRetval;
    }
}

Related

  1. deleteDirectory(String dirName)
  2. deleteDirectory(String dirname, boolean recursive)
  3. deleteDirectory(String fileName)
  4. deleteDirectory(String folder)
  5. deleteDirectory(String name)
  6. deleteDirectoryContents(File directory)
  7. deleteDirectoryContents(File directory)
  8. deleteDirectoryContents(File directory)
  9. deleteDirectoryContents(File directory)