BFS算法实现文件夹扫描

开发中很多时候不可避免对文件夹进行扫描,以下给出两种方案进行扫描文件夹,常用方案:递归扫描、BFS算法扫描

目录

一 、递归扫描

二、BFS算法扫描


一 、递归扫描

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * description:
 */
@Slf4j
public class ImageProducer3 extends Thread {

    private static final String ROOT_PATH = "";

    @Override
    public void run() {
        while (true) {
            getImage(ROOT_PATH);
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }

    }


    public void getImage(String filePath) {
        if (Objects.isNull(filePath)) {
            log.info("根路径为空");
            return;
        }
        File file = new File(filePath);
        File[] files = file.listFiles();
        if (files == null || files.length < 1) {
            return;
        }
        for (File f : files) {
            if (f.isDirectory()) {
                getImage(f.getAbsolutePath());
            } else if (f.isFile()) {
                log.info("文件:{}", f.getAbsolutePath());
            }
        }
    }

    public static void main(String[] args) {
        new ImageProducer3().start();
    }
}

二、BFS算法扫描

import lombok.extern.slf4j.Slf4j;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * description:
 */
@Slf4j
public class ImageProducer2 extends Thread {
    private static final String ROOT_PATH = "";
    Queue queue = new LinkedList<>();


    Set visited = new HashSet<>();

    @Override
    public void run() {
        queue.add(ROOT_PATH);
        while (true) {

            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    String poll = queue.poll();
                    if (visited.contains(poll)) {
                        continue;
                    }
                    boolean directory = Files.isDirectory(Paths.get(poll));
                    if (directory) {
                        try {
                            Files.list(Paths.get(poll)).forEach(f -> {
                                if (Files.isDirectory(f)) {
                                    log.info("文件夹:{}", f.getFileName().toString());
                                    if (!visited.contains(f.getFileName().toString())) {
                                        queue.add(f.toFile().getPath().toString());
                                    }
                                } else {
                                    log.info(f.getFileName().toString());
                                    visited.add(f.getFileName().toString());
                                }
                            });
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        visited.add(poll);
                        log.info("扫描文件:{}", poll);
                    }
                }

            }
            try {
                TimeUnit.MINUTES.sleep(6);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        ImageProducer2 imageProducer2 = new ImageProducer2();
        imageProducer2.start();
    }
}

你可能感兴趣的:(算法,算法,宽度优先)