基于Spring实现搜索目录下指定名称文件

基于Spring实现搜索目录下指定名称文件

一、简单需求

需要在指定目录下搜索指定名称文件列表

一般思路是通过递归遍历文件,然后通过过滤的方法去实现,

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.png");
        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-to-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文件搜索内容请搜索QQ沐编程以前的文章或继续浏览下面的相关文章希望大家以后多多支持QQ沐编程!

© 版权声明
THE END
喜欢就支持一下吧
点赞10赞赏 分享