package cn.bunny.file_dir;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo10 {
    public static void main(String[] args) throws IOException {
        String source = "D:\\BaiduNetdiskDownload\\web项目\\大屏模板\\【1849】";
        String target = "D:\\BaiduNetdiskDownload\\web项目\\大屏模板\\【1849】 aaaa";
        Files.walk(Paths.get(source)).forEach(path -> {
            try {
                String targetName = path.toString().replace(source, target);
                // 是目录 就创建
                if (Files.isDirectory(path)) {
                    Files.createDirectories(Paths.get(targetName));
                }
                // 是普通文件就复制
                else if (Files.isRegularFile(path)) {
                    Files.copy(path, Paths.get(targetName));
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        });
    }
    // 删除文件
    private static void m3() throws IOException {
        // 删除目录不允许，因为里面有文件，所以不可以删除，先将文件删除之后再删除文件夹
        Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板\\【1849】 - 副本"), new SimpleFileVisitor<>(){
            // 先删除文件
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }
            // 再删除文件夹
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    // 查看当前文件
    private static void m2() throws IOException {
        AtomicInteger atomicInteger = new AtomicInteger();
        Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板"), new SimpleFileVisitor<>(){
            /**
             * Invoked for a file in a directory.
             *
             * <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
             * CONTINUE}.
             *
             */
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.endsWith(".jpg")) {
                    System.out.println(file);
                    atomicInteger.incrementAndGet();
                }
                return super.visitFile(file, attrs);
            }
        });

        System.out.println("===>" + atomicInteger);
    }

    // 文件相关操作
    private static void m1() throws IOException {
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();

        // 访问者模式
        Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板"), new SimpleFileVisitor<>() {
            /**
             * Invoked for a directory before entries in the directory are visited.
             *
             * <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
             * CONTINUE}.
             *
             */
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("===>"+dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            /**
             * Invoked for a file in a directory.
             *
             * <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
             * CONTINUE}.
             *
             */
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("===>"+file);
                fileCount.decrementAndGet();
                return super.visitFile(file, attrs);
            }
        });

        System.out.println("文件夹的===>" + dirCount);
        System.out.println("文件的===>" + fileCount);
    }
}
