一旦你创建了遍历机制(实现 FileVisitor 接口或继承 SimpleFileVisitor 类),那么就可以调用 Files.walkFileTree() 方法遍历目录树。首先我们定义遍历机制:
class ListTree extends SimpleFileVisitor<Path> {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
System.out.println("Visited directory: " + dir.toString());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.out.println(exc);
return FileVisitResult.CONTINUE;
}
}
上面这段代码将会打印遍历过的目录名。
最简单的 walkFileTree() 方法接受两个参数,第一个参数表示开始遍历的文件(通常是文件树的根),第二个参数表示遍历机制。下面我们看看如何遍历 C:\rafaelnadal 目录树:
Path listDir = Paths.get("C:/rafaelnadal"); //define the starting file tree
ListTree walk = new ListTree(); //instantiate the walk
try{
Files.walkFileTree(listDir, walk); //start the walk
} catch(IOException e){
System.err.println(e);
}
walkFileTree() 还有一个比较复杂的重载方法,参数为:开始文件、自定义遍历选项、目录访问深度(为了确保遍历所有目录,可以设置 Integer.MAX_VALUE)、遍历机制。自定义选项值中可以使用 FileVisitOption 枚举类型,实际上这个枚举中只有一个常量 ,名为 FOLLOW_LINKS,表示在遍历的时候软链接将会被处理为目标文件(默认情况下,软链接不会被处理)。
调用这个方法的例子如下:
Path listDir = Paths.get("C:/rafaelnadal"); //define the starting file
ListTree walk = new ListTree(); //instantiate the walk
EnumSet opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS); //follow links
try{
Files.walkFileTree(listDir, opts, Integer.MAX_VALUE, walk); //start the walk
} catch(IOException e){
System.err.println(e);
}
注:调用 walkFileTree(start, visitor) 和调用 walkFileTree(start,
EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor) 的效果是一样的。
上面代码运行后可能的输出为:
Visited directory: C:\rafaelnadal\equipment
Visited directory: C:\rafaelnadal\grandslam\AustralianOpen
Visited directory: C:\rafaelnadal\grandslam\RolandGarros
Visited directory: C:\rafaelnadal\grandslam\USOpen
Visited directory: C:\rafaelnadal\grandslam\Wimbledon
Visited directory: C:\rafaelnadal\grandslam
…
Visited directory: C:\rafaelnadal
文章来源:
http://www.aptusource.org/2014/04/nio-2-recursive-process/
分享到:
相关推荐
Java I/O, NIO, and NIO.2 is a power-packed book that accelerates your mastery of Java's various I/O APIs. In this book, you'll learn about classic I/O APIs (File, RandomAccessFile, the stream classes ...
New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 ...
java nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava nio.pdfjava ...
这是一本介绍java io以及nio相关知识的书,书中对知识的讲解通俗易懂,是学习java nio以及复习java io相关知识的必备书籍。注意:本书为英文版!!!
Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf Java NIO.pdf
蔚来-NIO.US-新车上市,蔚来可期.pdf
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
Apress.Pro.Java.7.NIO.2.2011
Java NIO,Ron Hitchens 著,中文版 裴小星 译,Pro Java 7 NIO.2,Anghel Leonard 著,pdf文字版带书签,无安全限制
Pro Java 7 NIO.2.pdf,2011 by Anghel Leonard
Pro Java 7 NIO.2 – PDF Books
java NIO.zip
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies ...
JavaNIO.pdf
定义作为数据容器的缓冲区,并提供其他 NIO 包的概述。 NIO API 的集中抽象为: 缓冲区,它们是数据容器; 字符集 及其相关解码器 和编码器, 它们在字节和 Unicode 字符之间进行转换; 各种类型的通道,...
找了好久,终于找到了,java刷新同步获取网络资源
用java.nio.*进行网络编程
Java NIO.docx
nio的chm文档,非常不错,值得下载,赶紧下载吧