資料夾下檔案讀取並在記憶體中模糊查詢

2021-10-08 07:01:54 字數 3617 閱讀 6583

最近遇到乙個業務需求,覺得有點意思遂記錄。

需求如此:查詢指定資料夾下的所有檔案,並獲取檔案資訊,支援分頁、模糊查詢等。

分析:檔案在資料夾下,不在資料庫,因此讀取資料夾所有檔案,獲取檔案資訊,在記憶體中實現分頁、模糊查詢等功能。

首先得引入乙個包:

org.springframework<

/groupid>

spring-test<

/artifactid>

5.2.7

.release

<

/version>

<

/dependency>

這裡用到它的作用是,把file型別檔案轉換為multipartfile型別。

看**:

public r readfilesandquery

(map

map)

if(null != map && map.

get(

"start"

)!= null)

if(null != map && map.

get(

"filename"

)!= null)

try}

if(null != map && map.

get(

"endtime"

)!= null)

} list

list =

newarraylist

<

>()

; file file =

newfile

(filepaths);if

(null != file && file.

isdirectory()

)}string finalfilename = filename;

date finalstarttimereg = starttimereg;

date finalendtimereg = endtiemreg;

list

collect = list.

stream()

.filter

(item -

>

else

if(null == finalfilename)

/** *記憶體中的資料進行模糊查詢的方法

**/string regex =

".*"

+ finalfilename +

".*";if

(filerealname.

matches

(regex)

)return

false;}

).filter

(ite -

>

catch

(parseexception e)

//時間的比較:date型別,a.after(b) a在b之後

if(null != finalstarttimereg && finalstarttimereg.

after

(uploadtimereg)

)else

}return

true;}

).filter

(it -

>

catch

(exception e)

//時間的比較:date型別,a.before(b) a在b之前

if(null != finalendtimereg && finalendtimereg.

before

(uploadtimereg)

)else

}return

true;}

).collect

(collectors.

tolist()

);if(null != collect && collect.

size()

>0)

else

if(limit >0)

else

integer fromindex =

(start -1)

* limit;

integer toindex = start * limit;

//防止最後一頁不滿

if(toindex > total)

/** *擷取集合

* @param fromindex low endpoint (inclusive) of the sublist

* @param toindex high endpoint (exclusive) of the sublist

* 包含起始條,不包含最後一條

*/list

sublist = list.

sublist

(fromindex, toindex)

; data.

put(

"total"

, total)

; data.

put(

"pagesize"

, limit)

; data.

put(

"totalpage"

, totalpage)

; data.

put(

"currpage"

, start)

; data.

put(

"data"

, sublist);}

catch

(exception e)

return r.ok(

"查詢成功!").

put(

"data"

, data)

;}

①讀取資料夾下的所有檔案:file

.listfiles();

② file型別檔案轉換為multipartfile 型別(要匯入新包)

multipartfile multipartfile = new mockmultipartfile(f.getname(

),new fileinputstream(f)

)這裡僅僅是為了獲取檔案大小

③ 記憶體中的內容的模糊匹配

string regex =

".*"

+ finalfilename +

".*";if

(filerealname.matches(regex)

) ④ date型別的日期比較大小:a.before(b) a在b之前,a.

after

(b) a在b之後

⑤集合的擷取:包含起始條,不包含最後一條(因為集合是從第0開始的)

sublist:

@param fromindex low endpoint (inclusive)

of the sublist

@param toindex high endpoint (exclusive)

of the sublist

C 讀取資料夾下檔案,按照名字排序

正常的讀取檔案時非常簡單的,按照檔名排序就要參照windows的排序,往往新增排序處理後與windows的排序規則不統一,所以要學會呼叫windows 的排序dll,可以節省很多時間。if folder.exists 檔案的排序功能,目測sort使用的是氣泡排序 public class filen...

PHP遍歷資料夾下檔案

dir e video 這裡輸入其它路徑 php遍歷資料夾下所有檔案 handle opendir dir.echo 檔案 while false file readdir handle closedir handle 用了這段 遍歷所有檔案,幫我把所有檔名存為乙個陣列。s explode n tr...

C 遍歷資料夾下檔案

那麼到底如何查詢檔案呢?我們需要乙個結構體和幾個大家可能不太熟悉的函式。這些函式和結構體在的標頭檔案中,結構體為struct finddata t 函式為 findfirst findnext和 fineclose。具體如何使用,我會慢慢講來 首先講這個結構體吧 struct finddata t ...