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<String> queue = new LinkedList<>();


    Set<String> 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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值