java删除一个目录下面的所有文件_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java删除一个目录下面的所有文件

java删除一个目录下面的所有文件

 2013/7/17 12:13:33  javaWolf_1  程序员俱乐部  我要评论(0)
  • 摘要:java删除一个目录下面的所有文件如下程序:[java]viewplaincopyimportjava.io.File;publicclassDeleteAll{publicstaticvoiddeleteAll(Filefile){if(file.isFile()||file.list().length==0){file.delete();}else{File[]files=file.listFiles();for(inti=0;i<files.length;i++)
  • 标签:目录 文件 Java 一个
java删除一个目录下面的所有文件 class="link_categories">

如下程序:

[java]?view plaincopy ?
  1. import?java.io.File;??
  2. ??
  3. public?class?DeleteAll{??
  4. ??
  5. ?public?static?void?deleteAll(File?file){??
  6. ???
  7. ???if(file.isFile()?||?file.list().length?==0)??
  8. ???{??
  9. ???file.delete();???????
  10. ???}else{??????
  11. ?????File[]?files?=?file.listFiles();??
  12. ?????for?(int?i?=?0;?i?<?files.length;?i++)?{??
  13. ????deleteAll(files[i]);??
  14. ????files[i].delete();??????
  15. ???}??
  16. ??
  17. ????????
  18. ???????if(file.exists())?????????//如果文件本身就是目录?,就要删除目录??
  19. ???????file.delete();??
  20. ???}??
  21. ?}??
  22. ??
  23. ?public?static?void?main(String[]?args){???????
  24. ?????File?f?=?new?File("f:"+File.separator+"test");??
  25. ?????deleteAll(f);??
  26. ?}??
  27. }??

?

?

请不要轻易测试! 我的单元测试如下:

[java]?view plaincopy ?
  1. package?com.jzm;??
  2. ??
  3. import?java.io.File;??
  4. ??
  5. import?junit.framework.Assert;??
  6. ??
  7. import?junit.framework.TestCase;??
  8. ??
  9. ??
  10. public?class?DeleteAllTest?extends?TestCase{??
  11. ??
  12. ????public?void?testDeleteAll(){???//如果是一个文件??
  13. ??????????
  14. ????????File?file??=?null;??
  15. ??????????
  16. ????????try{??
  17. ??????????
  18. ????????file?=?new?File("f:\\test\\a.txt");??
  19. ??????????
  20. ????????file.createNewFile();??????????????
  21. ??????????
  22. ????????DeleteAll.deleteAll(file);??
  23. ??????????
  24. ????????}catch?(Exception?e)?{??
  25. ????????????//?TODO:?handle?exception??
  26. ????????}?????
  27. ????????Assert.assertFalse(file.exists());????????
  28. ????}??
  29. ??????
  30. ??????
  31. ????public?void?testDeleteAll2(){?????????//如果是一个一级目录??
  32. ??????????????
  33. ????????????File?directory??=?null;??
  34. ??????????????
  35. ????????????try{??
  36. ??????????????????????????
  37. ????????????directory??=?new?File("f:"+?File.separator?+"dirT2");??
  38. ???????????????
  39. ????????????directory.mkdir();??
  40. ??????????????
  41. ????????????File?file1?=?new?File(directory,?"t1.txt");??
  42. ??????????????
  43. ????????????File?file2?=?new?File(directory,?"t2.txt");??
  44. ??????????????
  45. ????????????file1.createNewFile();??
  46. ??????????????
  47. ????????????file2.createNewFile();????????????
  48. ??????????????
  49. ????????????DeleteAll.deleteAll(directory);???????
  50. ??????????????
  51. ????????????}catch?(Exception?e)?{??
  52. ??????????????????
  53. ????????????????//?TODO:?handle?exception?????????????????
  54. ????????????}?????????????????????
  55. ????????????Assert.assertFalse(directory.exists());???????
  56. ????????}??
  57. ??????
  58. ????public?void?testDeleteAll3(){??????????//如果是一个二级目录??
  59. ??????????
  60. ????????File?directory??=?null;??
  61. ??????????
  62. ????????try{??
  63. ??????????????????????
  64. ????????directory??=?new?File("f:"+?File.separator?+"dirT3");??
  65. ???????????
  66. ????????directory.mkdir();??
  67. ??????????
  68. ????????File??dir1??=?new?File(directory,"dir1");??
  69. ????????dir1.mkdir();??
  70. ??????????
  71. ????????File??dir2??=?new?File(directory,"dir2");??
  72. ????????dir2.mkdir();??
  73. ??????????
  74. ??????????
  75. ????????File?file1?=?new?File(dir1,?"t1.txt");??
  76. ??????????
  77. ????????File?file2?=?new?File(dir2,?"t2.txt");??
  78. ??????????
  79. ????????file1.createNewFile();??
  80. ??????????
  81. ????????file2.createNewFile();????????????
  82. ??????????
  83. ????????DeleteAll.deleteAll(directory);???????
  84. ??????????
  85. ????????}catch?(Exception?e)?{??
  86. ??????????????
  87. ????????????//?TODO:?handle?exception?????????????????
  88. ????????}?????????????
  89. ????????Assert.assertFalse(directory.exists());???????
  90. ????}??
  91. ??
  92. ??????
  93. } ?
发表评论
用户名: 匿名