使用Files.walkFileTree遍歷目錄文件
java.nio.file.Files.walkFileTree是JDK7新增的靜態(tài)工具方法。
1.Files.walkFileTree的原理介紹
static Path walkFileTree(Path start, Set<FileVisitOption> options, int maxDepth, FileVisitor<? super Path> visitor) throws IOException; static Path walkFileTree(Path start, FileVisitor<? super Path> visitor) throws IOException;
參數(shù)列表:
java.nio.file.Path start遍歷的起始路徑Set<java.nio.file.FileVisitOption> options遍歷選項(xiàng)int maxDepth遍歷深度java.nio.file.FileVisitor<? super Path> visitor遍歷過程中的行為控制器
2.遍歷行為控制器FileVisitor
接口java.nio.file.FileVisitor包含四個(gè)方法,涉及到遍歷過程中的幾個(gè)重要的步驟節(jié)點(diǎn)。
一般實(shí)際中使用SimpleFileVisitor簡(jiǎn)化操作。
public interface FileVisitor<T> {
FileVisitResult preVisitDirectory(T dir, BasicFileAttributes attrs)
throws IOException;
FileVisitResult visitFile(T file, BasicFileAttributes attrs)
throws IOException;
FileVisitResult visitFileFailed(T file, IOException exc)
throws IOException;
FileVisitResult postVisitDirectory(T dir, IOException exc)
throws IOException;
}preVisitDirectory訪問一個(gè)目錄,在進(jìn)入之前調(diào)用。postVisitDirectory一個(gè)目錄的所有節(jié)點(diǎn)都被訪問后調(diào)用。遍歷時(shí)跳過同級(jí)目錄或有錯(cuò)誤發(fā)生,Exception會(huì)傳遞給這個(gè)方法visitFile文件被訪問時(shí)被調(diào)用。該文件的文件屬性被傳遞給這個(gè)方法visitFileFailed當(dāng)文件不能被訪問時(shí),此方法被調(diào)用。Exception被傳遞給這個(gè)方法。
3.遍歷行為結(jié)果 FileVisitResult
public enum FileVisitResult {
CONTINUE,
TERMINATE,
SKIP_SUBTREE,
SKIP_SIBLINGS;
}CONTINUE繼續(xù)遍歷SKIP_SIBLINGS繼續(xù)遍歷,但忽略當(dāng)前節(jié)點(diǎn)的所有兄弟節(jié)點(diǎn)直接返回上一層繼續(xù)遍歷SKIP_SUBTREE繼續(xù)遍歷,但是忽略子目錄,但是子文件還是會(huì)訪問TERMINATE終止遍歷
4.查找指定文件
使用java.nio.file.Path提供的startsWith、endsWith等方法,需要特別注意的是:匹配的是路徑節(jié)點(diǎn)的完整內(nèi)容,而不是字符串。
例如: /usr/web/bbf.jar
Path path = Paths.get("/usr/web/bbf.jar");
path.endsWith("bbf.jar"); ?// true
path.endsWith(".jar"); ? ? // false5.使用PathMatcher
@Test
public void visitFile2() throws IOException {
// 查找java和txt文件
String glob = "glob:**/*.{java,txt}";
String path = "D:\\work_java\\bbf\\CORE";
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (pathMatcher.matches(file)) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
}getPathMatcher方法的參數(shù)語法:規(guī)則:模式,其中規(guī)則支持兩種模式glob和regex。
5.1全局規(guī)則glob
使用類似于正則表達(dá)式但語法更簡(jiǎn)單的模式,匹配路徑的字符串。
glob:*.java匹配以java結(jié)尾的文件glob:.匹配包含’.'的文件glob:*.{java,class}匹配以java或class結(jié)尾的文件glob:foo.?匹配以foo開頭且一個(gè)字符擴(kuò)展名的文件glob:/home//在unix平臺(tái)上匹配,例如/home/gus/data等glob:/home/**在unix平臺(tái)上匹配,例如/home/gus,/home/gus/dataglob:c:\\*在windows平臺(tái)上匹配,例如c:foo,c:bar,注意字符串轉(zhuǎn)義
5.1.1規(guī)則說明
*匹配零個(gè)或多個(gè)字符與名稱組件,不跨越目錄**匹配零個(gè)或多個(gè)字符與名稱組件,跨越目錄(含子目錄)?匹配一個(gè)字符的字符與名稱組件\轉(zhuǎn)義字符,例如{表示匹配左花括號(hào)[]匹配方括號(hào)表達(dá)式中的范圍,連字符(-)可指定范圍。例如[ABC]匹配"A"、“B"和"C”;[a-z]匹配從"a"到"z";[abce-g]匹配"a"、“b”、“c”、“e”、“f”、“g”;[!..]匹配范圍之外的字符與名稱組件,例如[!a-c]匹配除"a"、“b”、"c"之外的任意字符{}匹配組中的任意子模式,多個(gè)子模式用","分隔,不能嵌套。
5.2正則規(guī)則regex
使用java.util.regex.Pattern支持的正則表達(dá)式。
5.2.1示例
獲取指定擴(kuò)展名的文件
以下測(cè)試用例,目的都是獲取指定目錄下的.properties和.html文件。
/**
* 遞歸遍歷,字符串判斷
*
* @throws IOException IO異常
*/
@Test
public void visitFile1() throws IOException {
String path = "D:\\work_java\\hty\\HTY_CORE";
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
String pathStr = file.toString();
if (pathStr.endsWith("properties") || pathStr.endsWith("html")) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
}
/**
* 遞歸遍歷,glob模式
*
* @throws IOException IO異常
*/
@Test
public void visitFile2() throws IOException {
String glob = "glob:**/*.{properties,html}";
String path = "D:\\work_java\\hty\\HTY_CORE";
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (pathMatcher.matches(file)) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
}
/**
* 遞歸遍歷,正則模式
*
* @throws IOException IO異常
*/
@Test
public void visitFile3() throws IOException {
// (?i)忽略大小寫,(?:)標(biāo)記該匹配組不應(yīng)被捕獲
String reg = "regex:.*\\.(?i)(?:properties|html)";
String path = "D:\\work_java\\hty\\HTY_CORE";
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(reg);
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (pathMatcher.matches(file)) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
});
}6.查找指定文件
/**
* 查找指定文件
*
* @throws IOException IO異常
*/
@Test
public void visitFile() throws IOException {
String path = "D:\\work_java\\hty\\HTY_CORE\\src";
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
// 使用endsWith,必須是路徑中的一段,而不是幾個(gè)字符
if (file.endsWith("log.java")) {
System.out.println(file);
// 找到文件,終止操作
return FileVisitResult.TERMINATE;
}
return FileVisitResult.CONTINUE;
}
});
}7.遍歷單層目錄
使用DirectoryStream會(huì)獲取指定目錄下的目錄和文件??梢允褂胣ewDirectoryStream的第二個(gè)參數(shù)進(jìn)行篩選,glob語法。
/**
* 遍歷單層目錄
*
* @throws IOException IO異常
*/
@Test
public void dir() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src\\main\\resources");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(source, "*.xml")) {
Iterator<Path> ite = stream.iterator();
while (ite.hasNext()) {
Path pp = ite.next();
System.out.println(pp.getFileName());
}
}
}8.復(fù)制文件到新目錄
/**
* 遞歸復(fù)制
*
* @throws IOException IO異常
*/
@Test
public void copyAll() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src");
Path target = Paths.get("D:\\temp\\core");
// 源文件夾非目錄
if (!Files.isDirectory(source)) {
throw new IllegalArgumentException("源文件夾錯(cuò)誤");
}
// 源路徑的層級(jí)數(shù)
int sourcePart = source.getNameCount();
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
throws IOException {
// 在目標(biāo)文件夾中創(chuàng)建dir對(duì)應(yīng)的子文件夾
Path subDir;
if (dir.compareTo(source) == 0) {
subDir = target;
} else {
// 獲取相對(duì)原路徑的路徑名,然后組合到target上
subDir = target.resolve(dir.subpath(sourcePart, dir.getNameCount()));
}
Files.createDirectories(subDir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.copy(file, target.resolve(file.subpath(sourcePart, file.getNameCount())),
StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
});
System.out.println("復(fù)制完畢");
}9.文件和流的復(fù)制
/**
* 流復(fù)制到文件
*
* @throws IOException IO異常
*/
@Test
public void copy1() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src\\main\\resources\\ehcache.xml");
Path target = Paths.get("D:\\temp\\");
if (!Files.exists(target)) {
Files.createDirectories(target);
}
Path targetFile = target.resolve(source.getFileName());
try (InputStream fs = FileUtils.openInputStream(source.toFile())) {
Files.copy(fs, targetFile, StandardCopyOption.REPLACE_EXISTING);
}
}
/**
* 文件復(fù)制到流
*
* @throws IOException IO異常
*/
@Test
public void copy2() throws IOException {
Path source = Paths.get("D:\\work_java\\hty\\HTY_CORE\\src\\main\\resources\\ehcache.xml");
Path target = Paths.get("D:\\temp\\core");
Path targetFile = target.resolve(source.getFileName());
if (!Files.exists(target)) {
Files.createDirectories(target);
}
try (OutputStream fs = FileUtils.openOutputStream(targetFile.toFile());
OutputStream out = new BufferedOutputStream(fs)) {
Files.copy(source, out);
}
}
10.Path與File的轉(zhuǎn)換
/**
* Path與File的轉(zhuǎn)換
*/
@Test
public void testPath() {
File file = new File("D:\\work_java\\hty\\HTY_CORE");
System.out.println(file.toURI());//file:/D:/work_java/hty/HTY_CORE
System.out.println(file.getAbsolutePath());//D:\work_java\hty\HTY_CORE
System.out.println(file.getName());//HTY_CORE
System.out.println("-------");
//File轉(zhuǎn)換為Path
Path path = Paths.get(file.toURI());
System.out.println(path.toUri());//file:///D:/work_java/hty/HTY_CORE
System.out.println(path.toAbsolutePath());//D:\work_java\hty\HTY_CORE
System.out.println(path.getFileName());//HTY_CORE
System.out.println("-------");
//Path轉(zhuǎn)換為File
File f = path.toFile();
System.out.println(f.getAbsolutePath());//D:\work_java\hty\HTY_CORE
}以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽教程詳解
這篇文章主要介紹了zookeeper+Springboot實(shí)現(xiàn)服務(wù)器動(dòng)態(tài)上下線監(jiān)聽,主要介紹了什么是服務(wù)器動(dòng)態(tài)上下線監(jiān)聽及為什么要實(shí)現(xiàn)對(duì)服務(wù)器上下線的監(jiān)聽,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Kylin對(duì)接JDBC集成Zepplin的實(shí)現(xiàn)方法
Zepplin是一個(gè)非常好用的編輯器工具,通過自定義編碼可以實(shí)現(xiàn)更多的業(yè)務(wù)邏輯,接下來通過本文給大家分享Kylin對(duì)接JDBC和Zepplin的操作代碼,感興趣的朋友跟隨小編一起看看吧2021-05-05
JSP服務(wù)器端和前端出現(xiàn)亂碼問題解決方案
這篇文章主要介紹了JSP服務(wù)器端和前端出現(xiàn)亂碼問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Spring?Security中如何獲取AuthenticationManager對(duì)象
有時(shí)需要使用AuthenticationManager(以下簡(jiǎn)稱Manager)對(duì)象,可是這個(gè)對(duì)象不是Bean,沒有直接保存在Spring的Bean庫中,那么如何獲取Spring Security中的這個(gè)對(duì)象呢,需要的朋友可以參考下2022-11-11
Java基于Calendar類輸出指定年份和月份的日歷代碼實(shí)例
這篇文章主要介紹了Java 使用Calendar類輸出指定年份和月份的日歷,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
使用Java實(shí)現(xiàn)簡(jiǎn)單的區(qū)塊鏈程序的方法
這篇文章主要介紹了使用Java實(shí)現(xiàn)簡(jiǎn)單的區(qū)塊鏈程序的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
SpringAnimation 實(shí)現(xiàn)菜單從頂部彈出從底部消失動(dòng)畫效果
最近做項(xiàng)目遇到這樣一個(gè)需求,要求實(shí)現(xiàn)一種菜單,菜單從頂部彈入,然后從底部消失,頂部彈入時(shí),有一個(gè)上下抖動(dòng)的過程,底部消失時(shí),先向上滑動(dòng),然后再向下滑動(dòng)消失。下面給大家?guī)砹藢?shí)現(xiàn)代碼,感興趣的朋友一起看看吧2018-05-05
Spring Boot如何移除內(nèi)嵌Tomcat,使用非web方式啟動(dòng)
這篇文章主要介紹了Spring Boot如何移除內(nèi)嵌Tomcat,使用非web方式啟動(dòng),幫助大家更好的理解和學(xué)習(xí)使用spring boot框架,感興趣的朋友可以了解下2021-02-02

