1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| package com.colin.tool.function;
import com.colin.tool.file.FileSearchStream; import com.colin.tool.invokeableAnnotation.*;
import java.io.File; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Pattern;
@categoryInfo("文件搜索") public class FileSearch {
@functionInfo(value = "搜索整个文件夹", info = "列出文件夹内所有文件") public String searchFileByPath(@paramName("需要搜索的路径") String path) { AtomicInteger count = new AtomicInteger(); new FileSearchStream(new File(path)).forEach(file -> { System.out.println(file.getPath()); count.getAndIncrement(); }); return "搜索结束, 一共[" + count + "]个文件"; }
@functionInfo(value = "按文件名模糊搜索", info = "正则匹配路径内所有文件") public String searchFileLike(@paramName("搜索路径") String path, @paramName("文件名搜索正则规则") String regex) { AtomicInteger count = new AtomicInteger(); Pattern p = Pattern.compile(regex); new FileSearchStream(new File(path)).forEach(file -> { if(p.matcher(file.getName()).matches()) { System.out.println(file.getPath()); count.getAndIncrement(); } }); return "搜索结束, 一共[" + count + "]个文件"; }
@functionInfo(value = "按文件后缀搜索", info = "所搜指定后缀(结尾)的文件") public String searchFileBySuffix(@paramName("搜索路径") String path, @paramName("后缀") String suffix) { AtomicInteger count = new AtomicInteger(); new FileSearchStream(new File(path)).forEach(file -> { if(file.getName().endsWith(suffix)) { System.out.println(file.getPath()); count.getAndIncrement(); } }); return "搜索结束, 一共[" + count + "]个文件"; }
@functionInfo(value = "搜索指定类型的文件(img/document/application/audio/video)", info = "img[psd,pdd,gif,jpeg,jpg,png] document[doc,docx,xls,xlsx,csv,ppt,pptx,txt] application[exe,bat,cmd,sh,ink,py,class,java] audio[mp3,flac,ape,cd,wave,aiff] video[avi,mov,qt,asf,rm,navi,divX,mpeg,mpg,ogg,mod,rmvb,flv,mp4,3gp]") public String searchFileByType(@paramName("搜索路径") String path, @paramName("文件类型") String type) { FileSearchStream.FileSuffix suffix = null; switch (type.toLowerCase()) { case "img": suffix = FileSearchStream.FileSuffix.IMG; break; case "document": suffix = FileSearchStream.FileSuffix.DOCUMENT; break; case "application": suffix = FileSearchStream.FileSuffix.APPLICATION; break; case "audio": suffix = FileSearchStream.FileSuffix.AUDIO; break; case "video": suffix = FileSearchStream.FileSuffix.VIDEO; break; default: System.out.println("文件类型[" + type + "]未定义"); } AtomicInteger count = new AtomicInteger(); final FileSearchStream.FileSuffix checkSuffix = suffix; if(suffix != null) { new FileSearchStream(new File(path)).forEach(file -> { if(checkSuffix.check(file.getName())) { System.out.println(file.getPath()); count.getAndIncrement(); } }); } return "搜索结束, 一共[" + count + "]个文件"; } }
|