一、简单需求
需要在指定目录下搜索指定名称文件列表
一般思路是通过递归遍历文件,然后通过过滤的方法去实现,
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文件搜索内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论