递归删除本机指定类型文件

来源:互联网 发布:java文件上传到数据库 编辑:程序博客网 时间:2024/05/18 20:09


package mainutil;import java.io.File;public class IteratorD {private static int count = 0;/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString fstPath = IConst.path;IteratorD id = new IteratorD();id.Iteration(fstPath);id.print("Finish to Deleting All Folder,Count is: "+count);}/** * 递归处理所有目录中的文件 * @param path */private void Iteration(String path){//print("Now In Iteration ["+path+"]");File file = new File(path);if(!file.exists())return;if(file.isDirectory()){File[] childFiles = file.listFiles();if(childFiles==null||childFiles.length==0)return;for(File childFile:childFiles)Iteration(childFile.getAbsolutePath());}else if(file.canRead()){if(chkIfFormular(file.getName())){deleteFile(file);}}}/** * 检查后缀名是否符合期望格式 * @param name * @return */private boolean chkIfFormular(String name){if(name.endsWith(IConst.prefix1)||name.endsWith(IConst.prefix2))return true;return false;}/** * 删除指定文件操作 * @param file */private void deleteFile(File file){if(file==null){print("Deleting Null File!");return;}if(file.delete()){print("Succ ["+file.getName()+"] Deleted Successful");count++;}elseprint("Fail ["+file.getName()+"] Fail to Deleted!");}private void print(String info){System.out.println(info);}}


package mainutil;public class IConst {public static String path = "E:\\";public static String prefix1 = ".htm";public static String prefix2 = ".torrent";}