Here you can find the source of deleteDirectoryContent(final String strDir)
Parameter | Description |
---|---|
strDir | - string that specifies directory to delete |
public static boolean deleteDirectoryContent(final String strDir)
//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; } }