一、Files.walkFileTree() 遍历文件
walkFileTree()
包含用于递归遍历目录树的方法,方法参数接收一个Path对象和一个FileVisitor
对象,Path对象指向需要遍历的目录,FileVisitor
在遍历的时候调用。FileVisitor
接口定义如下:
public interface FileVisitor {
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException;
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException;
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException;
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
}
使用的时候需要实现FileVisitor
的接口
try {
Files.walkFileTree(start, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("pre visit dir:" + dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("visit file:" + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
System.out.println("visit file failed:" + file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("post visit dir:" + dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
输出:
pre visit dir:C:\Z_WORK\develop\1_GitHub\MyJava\nio-tutorial\nio-exam\target\classes\data
visit file:C:\Z_WORK\develop\1_GitHub\MyJava\nio-tutorial\nio-exam\target\classes\data\nio-data-to.txt
visit file:C:\Z_WORK\develop\1_GitHub\MyJava\nio-tutorial\nio-exam\target\classes\data\nio-data.txt
post visit dir:C:\Z_WORK\develop\1_GitHub\MyJava\nio-tutorial\nio-exam\target\classes\data
FileVisitor
接口中的方法会在不同的时间被调用:
- preVisitDirectory():在访问任意目录前调用;
- postVisitDirectory():在访问任意目录完成后调用;
- visitFile():在访问到每个文件时调用;
- visitFileFaild():在访问文件失败是调用
这四个方法都会返回一个FileVistResult
枚举类型的实例,FileVistResult
枚举包含四个选项:
- CONTINUE:继续遍历;
- TERMINATE:立即终止遍历;
- SKIP_SIBLINGS:跳过当前目录下的兄弟文件继续遍历;
- SKIP_SUBTREE:跳过当前目录继续遍历,这个只能在preVisitDirectory()方法中返回,在其他方法中返回和CONTINUE效果一样。
二、使用walkFileTree实现文件检索
private static void extracted4() {
Path start = Paths.get("D:/test");
try {
Files.walkFileTree(start, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String filePath = file.toAbsolutePath().toString();
if(filePath.endsWith("test1.txt")){
System.out.println("file found at path:" + filePath);
return FileVisitResult.CONTINUE;
}
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
三、使用walkFileTree递归删除目录
walkFileTree
还可以用来递归删除目录及其目录中所有文件,Files.delete()
只能删除空目录或者文件,当目中包含文件的时候无法直接删除,只能删除目录下所有文件后才能删除目录,walkFileTree
可以在isitFile()
方法中删除文件,然后在postVisitDirectory()
删除目录实现递归删除目录
Path start = Paths.get("D:/test/io");
try {
Files.walkFileTree(start, new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("delete file:" + file);
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
System.out.println("delete dir:" + dir);
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}