JAVA正則表達(dá)式過(guò)濾文件的實(shí)現(xiàn)方法

10年的烏拉特中網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整烏拉特中建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“烏拉特中網(wǎng)站設(shè)計(jì)”,“烏拉特中網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
正則表達(dá)式過(guò)濾文件列表,聽(tīng)起來(lái)簡(jiǎn)單,如果用java實(shí)現(xiàn),還真需要一番周折,本文簡(jiǎn)析2種方式
1、適用于路徑確定,文件名時(shí)正則表達(dá)式的情況(jdk6的寫(xiě)法)
String filePattern = "/data/logs/.+\\.log";
File f = new File(filePattern);
File parentDir = f.getParentFile();
String regex = f.getName();
FileSystem FS = FileSystems.getDefault();
final PathMatcher matcher = FS.getPathMatcher("regex:" + regex);
DirectoryStream.Filter<Path> fileFilter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return matcher.matches(entry.getFileName()) && !Files.isDirectory(entry);
}
};
List<File> result = Lists.newArrayList();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(parentDir.toPath(), fileFilter)) {
for (Path entry : stream) {
result.add(entry.toFile());
}
} catch (IOException e) {
e.printStackTrace();
}
for(File file : result) {
System.out.println(file.getParent() + "/" + file.getName());
}
2、適用于路徑確定,文件名正則表達(dá)式的情況,這種正則表達(dá)式是JAVA支持的表達(dá)式,而非系統(tǒng)(unix)文件系統(tǒng)表達(dá)式(jdk8寫(xiě)法)
Path path = Paths.get("/data/logs");
Pattern pattern = Pattern.compile("^.+\\.log");
List<Path> paths = Files.walk(path).filter(p -> {
//如果不是普通的文件,則過(guò)濾掉
if(!Files.isRegularFile(p)) {
return false;
}
File file = p.toFile();
Matcher matcher = pattern.matcher(file.getName());
return matcher.matches();
}).collect(Collectors.toList());
for(Path item : paths) {
System.out.println(item.toFile().getPath());
}
以上就是java 正則表達(dá)式過(guò)濾文件的實(shí)例,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
當(dāng)前文章:JAVA正則表達(dá)式過(guò)濾文件的實(shí)現(xiàn)方法
標(biāo)題URL:http://chinadenli.net/article28/gdgecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、域名注冊(cè)、、軟件開(kāi)發(fā)、服務(wù)器托管、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)