Delete Folder Tool class Deletefolder.java

Source: Internet
Author: User

  1. Package com.util;
  2. Import Java.io.File;
  3. /**
  4. * Delete Folder
  5. * @createTime DSC 20, 2010 15:38
  6. * @version 2.0
  7. */
  8. Public class DeleteFolder {
  9. //Delete folder
  10. //param FolderPath folder complete absolute path
  11. public static void Delfolder (String folderpath) {
  12. try {
  13. Delallfile (FolderPath); //Delete all content inside
  14. String FilePath = FolderPath;
  15. FilePath = Filepath.tostring ();
  16. Java.io.File Myfilepath = new Java.io.File (FilePath);
  17. Myfilepath.delete (); //Delete empty folders
  18. } catch (Exception e) {
  19. E.printstacktrace ();
  20. }
  21. }
  22. //Delete all files under the specified folder
  23. //param path folder complete absolute path
  24. public Static boolean delallfile (String path) {
  25. Boolean flag = false;
  26. File File = new file (path);
  27. if (!file.exists ()) {
  28. return flag;
  29. }
  30. if (!file.isdirectory ()) {
  31. return flag;
  32. }
  33. string[] templist = File.list ();
  34. File temp = null;
  35. For (int i = 0; i < templist.length; i++) {
  36. if (Path.endswith (File.separator)) {
  37. temp = New File (path + templist[i]);
  38. } Else {
  39. temp = New File (path + file.separator + templist[i]);
  40. }
  41. if (Temp.isfile ()) {
  42. Temp.delete ();
  43. }
  44. if (temp.isdirectory ()) {
  45. Delallfile (path + "/" + templist[i]); Delete the files inside the folder first
  46. Delfolder (path + "/" + templist[i]); Then delete the empty folder
  47. Flag = true;
  48. }
  49. }
  50. return flag;
  51. }
  52. }

Delete Folder Tool class Deletefolder.java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.