Remove a directory and all of its contents. : Delete « File « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » File » Delete 




import java.io.File;
import java.util.StringTokenizer;

/*

 Derby - Class org.apache.derby.iapi.util.PropertyUtil

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to you under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

public class Main {

  /**
  Remove a directory and all of its contents.

  The results of executing File.delete() on a File object
  that represents a directory seems to be platform
  dependent. This method removes the directory
  and all of its contents.

  @return true if the complete directory was removed, false if it could not be.
  If false is returned then some of the files in the directory may have been removed.

*/
public static boolean removeDirectory(File directory) {

  // System.out.println("removeDirectory " + directory);

  if (directory == null)
    return false;
  if (!directory.exists())
    return true;
  if (!directory.isDirectory())
    return false;

  String[] list = directory.list();

  // Some JVMs return null for File.list() when the
  // directory is empty.
  if (list != null) {
    for (int i = 0; i < list.length; i++) {
      File entry = new File(directory, list[i]);

      //        System.out.println("\tremoving entry " + entry);

      if (entry.isDirectory())
      {
        if (!removeDirectory(entry))
          return false;
      }
      else
      {
        if (!entry.delete())
          return false;
      }
    }
  }

  return directory.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.