当前位置: 代码网 > it编程>编程语言>Java > 基于Spring实现搜索目录下指定名称文件

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

2025年08月03日 Java 我要评论
一、简单需求需要在指定目录下搜索指定名称文件列表一般思路是通过递归遍历文件,然后通过过滤的方法去实现,spring的pathmatchingresourcepatternresolver 给了我们另外

一、简单需求

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

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

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文件搜索内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com