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

如下程序:

[java]  view plain copy
  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 plain copy
  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. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值