闲着无聊,写了个小程序。

  需求:

       举例: 比如我的E盘有一个test的目录,test的结构如下:

 

 

【原创】java删除未匹配的文件夹FileFileFilter,FileUtils,删除目录名字不是某个名字的所有文件夹及其子文件夹

要求除了包含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 }
View FileDelete Code

相关文章:

  • 2021-08-29
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2022-12-23
  • 2022-01-28
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
相关资源
相似解决方案