基于Spring实现搜索目录下指定名称文件
目录
- 一、简单需求
- 二、源码实现
- 三、运行结果android
一、简单需求
需要在指定目录下编程客栈搜索指定名称文件列表
一般思路是通过递归遍历文件,然后通过过滤的方法去实现,
spring的PathMatchingResourcePatternResolver
给了我们另外一种简单实现。
二、源码实现
import Java.io.IOException; import java.util.Arrays; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import lombok.extern.slf4j.Slf4j; @Slf4j public class FileListBySpring { PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); String userProfile = "file:" + System.getenv().get("USERPROFILE"); /** * 遍历文件 * * @throws IOException */ @Test public void testList() throws IOException { // 外部文件 Resource[] jpgs = resolver.getResources(userProfile + "/Pictures/**/*.jpg"); Arrays.stream(jpgs).forEach(System.out::println); // 外部文件 Resource[] pngs = resolver.getResources(userProfile + "/Pictures/**/*.png"); Arrays.stream(pngs).forEach(System.out::println); // 工程或jar内文件 Resource[] XMLs = resolver.getResources("classpath*:**/*.xml"); Arrays.stream(xmls).forEach(System.out::println); // 合并 log.info("################################################## 合并后 ##################################################"); Stream.of(jpgs, pngs, xmls).flatMap(Arrays::stream).forEach(System.out::println); } /** * 遍历文件,支持指定文件名搜索 * * @throws IOException */ @Test public void testList2() throws IOException { Resource[] pngs = resolver.getResources(userProfile + "/Pictures/**/001.wHxaYxwmqJpng"); Arrays.stream(pngs).forEach(System.out::println); Resource[] pngs2 = resolver.getResources(userProfile + "/Pictures/**/00*.PNG"); Arrays.stream(pngs2).forEach(System.out::println); Resource[] xmls 编程客栈= resolver.getResources("file:C:/Gitee/00fly/effict-side/**/pom.xml"); Arrays.stream(xmls).forEach(System.out::println); } }
三、运行结果
testList2
file [C:\Users\Administrator\Pictures\001.png]
file [C:\Users\Administrator\Pictures\eclipse\001.png]file [C:\Users\Administrator\Pictures\00006.PNG]file [C:\Users\Administrator\Pictures\00007.PNG]file [C:\Users\Administrator\Pictures\00011.PNG]file [C:\Users\Administrator\Pictures\0002.PNG]file [C:\Users\Administrator\Pictures\0003.PNG]file [C:\Users\Administrator\Pictures\0004.PNG]file [C:\Users\Administrator\Pictures\0005.PNG]file [C:\Users\Administrator\Pictures\0008.PNG]file [C:\Users\Administrator\Pictures\0010.PNG]file [C:\Users\Administrator\Pictures\009.PNG]file [C:\Users\Administrator\Pictures\eclipse\000.PNG]file [C:\Users\Administrator\Pictures\eclipse\006.PNG]file [C:\Gitee\00fly\effict-side\apidoc-image\pom.xml]file [C:\Gitee\00fly\effict-side\auto-to-swagger\JSP-tandroido-swagger\pom.xml]file [C:\Gitee\00fly\effict-side\class-junit-run\java-demo\pom.xml]file [C:\Gitee\00fly\effict-side\class-junit-run\java-junit4\pom.xml]file [C:\Gitee\00fly\effict-side\class-junit-run\run-test-boot\pom.xml]file [C:\Gitee\00fly\effict-side\class-junit-run\run-test-simple\pom.xml]file [C:\Gitee\00fly\effict-side\class-junit-run\tcp-boot\pom.xml]file [C:\Gitee\00fly\effict-side\class-junit-run\tcp-java\pom.xml]。。。。。。
到此这篇关于基于Spring实现搜索目录下指定名称文件的文章就介绍到这了,更多相关Spring文件搜索内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论