import java.io.File;
/**
* 运用IO流知识,编一个程序,将某一个目录下所有文件删除
* @author 够潮
*
*/
public class Demo9 {
/**
* 利用递归遍历
* @param filePath
*/
public static void deleteAllFile(String filePath){
/**
* 绑定目录
*/
File dir = new File(filePath);
/**
* 得到子目录和文件列表
*/
String fileList[] = dir.list();
/**
* 遍历
*/
if(fileList == null)
return ;
for( int i= 0 ; i < fileList.length; i ++){
String name = fileList[i];
String pathName = filePath+File.separator+name;//得到当前目录的全路径
File curFile = new File(pathName);
/**
* 如果当前是目录
*/
if(curFile.isDirectory() ){
//System.out.println(pathName+":");
/**
* 递归
*/
deleteAllFile(pathName);
}
/**
* 如果当前是文件
*/
if(curFile.isFile()){
if(curFile.delete())
System.out.println("成功删除文件:"+pathName) ;
}
}
if(dir.delete())
System.out.println("成功删除目录: " + filePath );
}
/**
* @param args
*/
public static void main(String[] args) {
Demo9.deleteAllFile("D://beautygroup");
}
}