访问者,顾名思义,就是对象访问的一种设计模式,我们可以在不改变要访问对象的前提下,对访问对象的操作做拓展,包括访问前,访问中,访问后
public static void main(String[] args) throws IOException {
//由于匿名内部类只能访问不变变量,即final,因此使用原子整数计数
AtomicInteger fileCount = new AtomicInteger();
AtomicInteger dirCount = new AtomicInteger();
Files.walkFileTree(Paths.get("D:\\nginx\\nginx-1.22.0"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("目录:" + dir);
dirCount.getAndIncrement();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("文