递归删除文件及目录

 

 
 
  1. package com.test; 
  2.  
  3. import java.io.File; 
  4.  
  5. public class DeleteFileDirectory { 
  6.  
  7.     /** 
  8.      * 删除指定目录及下面的文件 
  9.      */ 
  10.     public static void main(String[] args) { 
  11.         deleteFile("c:\\TestCopy"); 
  12.     } 
  13.     private static void deleteFile(String path){ 
  14.         File[] file = new File(path).listFiles(); 
  15.         for (File file2 : file) { 
  16.             if(file2.isDirectory()){ 
  17.                 System.out.println(file2.getPath()); 
  18.                 if(file2.listFiles().length==0){ 
  19.                     file2.delete(); 
  20.                 }else
  21.                     deleteFile(file2.getPath()); 
  22.                 } 
  23.             }else
  24.                 System.out.println(file2); 
  25.                 file2.delete(); 
  26.             } 
  27.         } 
  28.     } 
  29.  

过程结果

 
 
  1. c:\TestCopy\a 
  2. c:\TestCopy\a\1.xml 
  3. c:\TestCopy\b 
  4. c:\TestCopy\b\2.xml 

 本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/568563,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值