基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件
一、簡單需求
需要在指定目錄下搜索指定名稱文件列表
一般思路是通過遞歸遍歷文件,然后通過過濾的方法去實(shí)現(xiàn),
spring的PathMatchingResourcePatternResolver 給了我們另外一種簡單實(shí)現(xiàn)。
二、源碼實(shí)現(xiàn)
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內(nèi)文件
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);
}
}
三、運(yùn)行結(jié)果
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]
。。。。。。
到此這篇關(guān)于基于Spring實(shí)現(xiàn)搜索目錄下指定名稱文件的文章就介紹到這了,更多相關(guān)Spring文件搜索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成ElasticSearch實(shí)現(xiàn)搜索功能
- SpringBoot實(shí)現(xiàn)海量數(shù)據(jù)高效實(shí)時(shí)搜索功能
- SpringBoot+Elasticsearch實(shí)現(xiàn)數(shù)據(jù)搜索的方法詳解
- springboot結(jié)合redis實(shí)現(xiàn)搜索欄熱搜功能及文字過濾
- 詳解Elastic Search搜索引擎在SpringBoot中的實(shí)踐
- Spring Boot整合Elasticsearch實(shí)現(xiàn)全文搜索引擎案例解析
- Spring Boot集成ElasticSearch實(shí)現(xiàn)搜索引擎的示例
相關(guān)文章
SpringBoot之groups應(yīng)對不同的Validation規(guī)則自定義方式
這篇文章主要介紹了SpringBoot之groups應(yīng)對不同的Validation規(guī)則自定義方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java實(shí)戰(zhàn)之醫(yī)院管理系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用Java實(shí)現(xiàn)醫(yī)院管理系統(tǒng),文中用到的技術(shù)有:SpringBoot、Layui、Freemaker等,感興趣的同學(xué)可以了解一下2022-04-04
Mybatis動態(tài)調(diào)用表名和字段名的解決方法
今天在項(xiàng)目開發(fā)中有個(gè)業(yè)務(wù)是需要限制各個(gè)用戶對某些表里的字段查詢以及某些字段是否顯示,這種情況下,就需要構(gòu)建sql來動態(tài)傳入表名、字段名了,下面給大家介紹mybatis動態(tài)調(diào)用表名和字段名的解決方法,一起看看吧2016-10-10
springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁上的支付和退款
最近做項(xiàng)目需要實(shí)現(xiàn)在pc端需要實(shí)現(xiàn)微信的支付,本文主要介紹了springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁上的支付和退款,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
Mybatis-plus 雙主鍵的實(shí)現(xiàn)示例
本文主要介紹了Mybatis-plus 雙主鍵的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05
maven打包時(shí)配置多環(huán)境參數(shù)的實(shí)現(xiàn)
本文主要介紹了maven打包時(shí)配置多環(huán)境參數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04

