Recursive directory deletion in Java

Description

The following code shows how to recursive directory deletion.

Example


/*w  w w  .java  2 s.co  m*/
import java.io.File;

public class Main {
  public static boolean delete(String filePath, boolean recursive) {
      File file = new File(filePath);
      if (!file.exists()) {
          return true;
      }

      if (!recursive || !file.isDirectory())
          return file.delete();

      String[] list = file.list();
      for (int i = 0; i < list.length; i++) {
          if (!delete(filePath + File.separator + list[i], true))
              return false;
      }

      return file.delete();
  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip