Delete all files under this file and including this file : Delete « File « Java Tutorial

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
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » File » Delete 
11. 79. 3. Delete all files under this file and including this file
import java.io.File;
import java.io.IOException;

public class Utils {
  /**
   * delete all files under this file and including this file
   
   @param f
   @throws IOException
   */
  public static void deleteAll(File fthrows IOException {
    recurse(f, new RecurseAction() {
      public void doFile(File filethrows IOException {
        file.delete();
        if (file.exists()) {
          throw new IOException("Failed to delete  " + file.getPath());
        }
      }

      public void doBeforeFile(File f) {
      }

      public void doAfterFile(File f) {
      }
    });
  }

  public static void recurse(File f, RecurseAction actionthrows IOException {
    action.doBeforeFile(f);
    if (f.isDirectory()) {
      File[] files = f.listFiles();
      if (files != null) {
        for (int i = 0; i < files.length; i++) {
          if (files[i].isDirectory()) {
            recurse(files[i], action);
          else {
            action.doFile(files[i]);
          }
        }
      }
    }
    action.doFile(f);
  }
}

interface RecurseAction {

  /**
   @param file
   @throws IOException
   */
  void doFile(File filethrows IOException;

  /**
   @param f
   */
  void doBeforeFile(File f);

  /**
   @param f
   */
  void doAfterFile(File f);

}
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.