【原创】java删除未匹配的文件夹FileFileFilter,FileUtils,删除目录名字不是某个名字的所有文件夹及其子文件夹
闲着无聊,写了个小程序。
需求:
举例: 比如我的E盘有一个test的目录,test的结构如下:
要求除了包含hello的目录不删除以外,其他的所有文件夹都要删除。
代码如下:
1 package com.zuishiming.filedelete; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.Collection; 7 8 import org.apache.commons.io.FileUtils; 9 import org.apache.commons.io.filefilter.DirectoryFileFilter; 10 import org.apache.commons.io.filefilter.FileFileFilter; 11 import org.apache.commons.io.filefilter.IOFileFilter; 12 13 /** 14 * Requirement: we want to delete all dirs except a SPECIALNAME dir in one root directory. </br> 15 * tree data: </br> 16 * rootdir(c:\test) </br> 17 * --dir1</br> 18 * --dir11 </br> 19 * --dir12("hello") </br> 20 * --dir13</br> 21 * --dir2</br> 22 * --dir21</br> 23 * --dir211</br> 24 * --dir2111("hello")</br> 25 * --dir212</br> 26 * --dir22</br> 27 * --dir3</br> 28 * as this case, we want to delete c:\test dir all dirs except dir name is "hello", just bellow two dirs</br> 29 * c:\test\dir1\dir12 && c:\test\dir2\dir21\dir211\dir2111</br> 30 * 31 * @author 草原战狼 32 * 33 */ 34 public class FileDelete { 35 public static void main(String[] args) throws IOException { 36 System.out.println("FileDelete Usage:"); 37 System.out.println("java -jar DeleteDirsAddFilter.jar wantDeleteParentDir keepDirectoryOrFiles"); 38 System.out.println("for example: java -jar DeleteDirsAddFilter.jar c:\\ \"test\""); 39 if (args.length != 2) { 40 System.out.println("parameters length must be 2"); 41 System.exit(0); 42 } 43 // File wantDeleteDir = new File("E:\\test\\"); 44 // String filterName = "hello"; 45 File wantDeleteDir = new File(args[0]); 46 String filterName = args[1]; 47 if (!wantDeleteDir.isDirectory()) { 48 System.out.println(wantDeleteDir.getPath() + " is not a directory"); 49 System.exit(0); 50 } 51 IOFileFilter filterDeleteDir = DirectoryFileFilter.DIRECTORY; 52 FileFileFilter fileFiter = (FileFileFilter) FileFileFilter.FILE; 53 Collection<File> filesAndDirs = FileUtils.listFilesAndDirs( 54 wantDeleteDir, fileFiter, filterDeleteDir); 55 System.out.println("Total Files/Dirs in " + wantDeleteDir.getPath() +": " + filesAndDirs.size()); 56 int countFile = 0; 57 ArrayList<FileMatchPath> allLeafDirs = listAllChildDirs(wantDeleteDir); 58 System.out.println("Total Dirs in " + wantDeleteDir.getPath() +": " + allLeafDirs.size()); 59 FileMatchPath toFile = new FileMatchPath(); 60 FileMatchPath tempToFile = new FileMatchPath(); 61 ArrayList<FileMatchPath> resultFileMatch = new ArrayList<FileMatchPath>(); 62 for (FileMatchPath leafDir : allLeafDirs) { 63 if (isNotDeltedFile(leafDir.getFile(), filterName)) { 64 countFile++; 65 String toFilePath = wantDeleteDir.getParentFile().getAbsolutePath() + "\\temp11" + countFile; 66 File toFileFile = new File(toFilePath); 67 toFile.setFile(toFileFile); 68 if(leafDir.getFile().exists()) { 69 tempToFile = moveMatchFiles(leafDir, toFile); 70 resultFileMatch.add(tempToFile); 71 } 72 } 73 } 74 FileUtils.deleteDirectory(wantDeleteDir); 75 wantDeleteDir.mkdir(); 76 System.out.println("Total contains " + filterName + " size: " + resultFileMatch.size()); 77 if(resultFileMatch.size() != 0) { 78 for(FileMatchPath fromFile : resultFileMatch) { 79 File toFileFile = new File(fromFile.getPath()); 80 FileUtils.moveDirectory(fromFile.getFile(), toFileFile); 81 System.out.println("contain file: " + toFileFile.getAbsolutePath()); 82 } 83 } 84 System.out.println("All Files handle successful"); 85 } 86 87 /** 88 * validate a dir's name is equals to <code>filterName</code> 89 * @param dir 90 * @param filterName 91 * @return 92 */ 93 private static boolean isNotDeltedFile(File dir, String filterName) { 94 if (dir.isDirectory() && dir.getName().equals(filterName)) { 95 return true; 96 } 97 return false; 98 } 99 100 /** 101 * move dir to a new destination, 102 * this file get from a <code>FileMatchPath</code>, and store from file's path to new toDirMatchPath 103 * @param fromDirMatchPath 104 * @param toDirMatchPath 105 * @return 106 * @throws IOException 107 */ 108 private static FileMatchPath moveMatchFiles(FileMatchPath fromDirMatchPath, FileMatchPath toDirMatchPath) throws IOException { 109 FileUtils.moveDirectory(fromDirMatchPath.getFile(), toDirMatchPath.getFile()); 110 FileMatchPath destFileMatchPath = new FileMatchPath(toDirMatchPath.getFile(), fromDirMatchPath.getPath()); 111 return destFileMatchPath; 112 } 113 114 /** 115 * List all dirs in rootDir 116 * @param rootDir find its child dirs 117 * @return all <code>rootDir</code>'s child dirs 118 */ 119 private static ArrayList<FileMatchPath> listAllChildDirs(File rootDir) { 120 ArrayList<FileMatchPath> leafDirs = new ArrayList<FileMatchPath>(); 121 IOFileFilter filterDeleteDir = DirectoryFileFilter.DIRECTORY; 122 FileFileFilter fileFiter = (FileFileFilter) FileFileFilter.FILE; 123 Collection<File> filesAndDirs = FileUtils.listFilesAndDirs( 124 rootDir, fileFiter, filterDeleteDir); 125 for(File file : filesAndDirs) { 126 if(file.isDirectory()) { 127 FileMatchPath fileMatch = new FileMatchPath(); 128 fileMatch.setFile(file); 129 fileMatch.setPath(file.getAbsolutePath()); 130 leafDirs.add(fileMatch); 131 } 132 } 133 return leafDirs; 134 } 135 }
这个类是主要方法,步骤首先找到那些符合条件的文件夹,然后把他们复制到其他地方,再删除当前目录,再把那些目录复制回来。
整体过程比较简单,我本来想用数据结构树的玩法来弄,但是没想出什么比较的方法,就比较土的这么玩了。
但是这么玩,也有个问题要解决,那就是,需要记住被复制出去的那些文件夹的最初始路径,因为后面还要复制回来。因此,我加了个FileMatchPath类。
这个类映射了文件和路径,这样的话,我就能把之前的路径给保存起来了。FileMatchPath类的代码。
1 package com.zuishiming.filedelete; 2 3 import java.io.File; 4 5 /** 6 * 7 * store file and path match 8 * the path can be file's path or 9 * other file's path 10 * @author 草原战狼 11 * 12 */ 13 public class FileMatchPath{ 14 private File file; 15 private String path; 16 public File getFile() { 17 return file; 18 } 19 public void setFile(File file) { 20 this.file = file; 21 } 22 public String getPath() { 23 return this.path; 24 } 25 public void setPath(String path) { 26 this.path = path; 27 } 28 public FileMatchPath() { 29 } 30 public FileMatchPath(File file, String path) { 31 this.file = file; 32 this.path = path; 33 } 34 }
这样的话,做完就可以Export出来了,我Export出来的可执行jar文件为:DeleteDirsAddFilter.jar
通过java执行:
执行后的结果如下:
这样的话,表示我们成功了。
草原战狼淘宝小店:http://xarxf.taobao.com/ 淘宝搜小矮人鞋坊,主营精致美丽时尚女鞋,为您的白雪公主挑一双哦。谢谢各位博友的支持。
===============================================================================
========================== 以上分析仅代表个人观点,欢迎指正与交流 ==========================
========================== 尊重劳动成果,转载请注明出处,万分感谢 ==========================
===============================================================================