`

删除指定目录下的所有文件的java类

阅读更多

publicclass DelFile{
/**
* 删除指定目录下全部文件和目录
* @param filePath 需删除的文件目录路径
*/
public static void delAllFile(String filePath)
{
/**
* 指定删除目录路径构造一个文件对象

*/
File file = new File(filePath);

File[] fileList = file.listFiles();
/**
* 初始化子目录路径
*/
String dirPath = null;

if(fileList != null)
for(int i = 0 ; i < fileList.length; i++)
{
/**
* 如果是文件就将其删除
*/
if(fileList[i].isFile())
fileList[i].delete();
/**
* 如果是目录,那么将些目录下所有文件删除后再将其目录删除,
*/
if(fileList[i].isDirectory()){

dirPath = fileList[i].getPath();
//递归删除指定目录下所有文件

delAllFile(dirPath);
}
}
/**
* 删除给定根目录
*/
file.delete();
}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics