Delete the file or non-empty directory at the supplied path : Delete « File « Java Tutorial






/*
 * JBoss DNA (http://www.jboss.org/dna)
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * See the AUTHORS.txt file in the distribution for a full listing of 
 * individual contributors. 
 *
 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
 * is licensed to you under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * JBoss DNA 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.File;

public class FileMonitor {

  /**
   * Delete the file or directory at the supplied path. This method works on a directory that is not empty, unlike the
   * {@link File#delete()} method.
   * 
   * @param path the path to the file or directory that is to be deleted
   * @return true if the file or directory at the supplied path existed and was successfully deleted, or false otherwise
   */
  public static boolean delete( String path ) {
      if (path == null || path.trim().length() == 0) return false;
      return delete(new File(path));
  }

  /**
   * Delete the file or directory given by the supplied reference. This method works on a directory that is not empty, unlike
   * the {@link File#delete()} method.
   * 
   * @param fileOrDirectory the reference to the Java File object that is to be deleted
   * @return true if the supplied file or directory existed and was successfully deleted, or false otherwise
   */
  public static boolean delete( File fileOrDirectory ) {
      if (fileOrDirectory == null) return false;
      if (!fileOrDirectory.exists()) return false;

      // The file/directory exists, so if a directory delete all of the contents ...
      if (fileOrDirectory.isDirectory()) {
          for (File childFile : fileOrDirectory.listFiles()) {
              delete(childFile); // recursive call (good enough for now until we need something better)
          }
          // Now an empty directory ...
      }
      // Whether this is a file or empty directory, just delete it ...
      return fileOrDirectory.delete();
  }
}








11.79.Delete
11.79.1.Deletes the diretory and any files and directories in it recursively.
11.79.2.Delete the file or non-empty directory at the supplied path
11.79.3.Delete all files under this file and including this file
11.79.4.Deletes a directory.
11.79.5.Empty and delete a folder (and subfolders).
11.79.6.Recursive directory deletion
11.79.7.Recursively delete a file and all its contents
11.79.8.Recursivly delete directory
11.79.9.Remove file or directory
11.79.10.Remove a directory and all of its contents.
11.79.11.Deletes all files and subdirectories