深度优先与广度优先遍历文件

package agrisom;

import java.io.File;
import java.io.IOException;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;

public class ListFile {

    public static void main(String[] args) throws IOException {
        File file = new File("C://Program Files//Common Files//System");
        DFSWithStack(file);
        System.out.println("--------------------------------");
        DFSWithStack2(file);
        System.out.println("--------------------------------");
        DFS(file);
        System.out.println("--------------------------------");
        BFSWithQueue(file);
    }

    /**
     * 深度非递归遍历文件
     * 缺点是对于同一层次总是先输出所有的文件(子节点),再进行子目录的深度遍历,不太符合顺序
     */
    private static void DFSWithStack(File file) throws IOException {
        Stack<File> stack = new Stack<File>();
        stack.push(file);
        File fileInStack = null;
        while (!stack.isEmpty()) {
            fileInStack = stack.pop();
            System.out.println(fileInStack.getCanonicalPath());
            File[] files = fileInStack.listFiles();
            for (File eachFile : files) {
                if (eachFile.isFile()) {
                    System.out.println(eachFile.getCanonicalPath());
                } else {
                    stack.push(eachFile);
                }
            }
        }
    }
    /**
     * 深度非递归遍历文件
     * 保证遍历顺序
     */
    private static void DFSWithStack2(File file) throws IOException {
        Stack<File> stack = new Stack<File>();
        stack.push(file);
        File fileInStack = null;
        while (!stack.isEmpty()) {
            fileInStack = stack.pop();
            System.out.println(fileInStack.getCanonicalPath());
            File[] files = fileInStack.listFiles();
            for (int i=0;files!=null && i<files.length;i++) {
                File eachFile=files[i];
                if (eachFile.isFile()) {
                    System.out.println(eachFile.getCanonicalPath());
                } else {
                    //只要访问到子目录,就需要将所有后续子节点入栈
                    for(int j=i;j<files.length;j++){
                        stack.push(files[j]);
                    }
                    break;
                }
            }
        }
    }

    //深度递归遍历文件
    private static void DFS(File file) throws IOException {
        System.out.println(file.getCanonicalPath());
        File[] files = file.listFiles();
        for (File eachFile : files) {
            if (eachFile.isFile()) {
                System.out.println(eachFile.getCanonicalPath());
            } else {
                DFS(eachFile);
            }
        }
    }

    //广度非递归遍历文件
    private static void BFSWithQueue(File file) throws IOException {
        System.out.println(file.getCanonicalPath());
        Queue<File> queue = new LinkedList<File>();
        queue.offer(file);
        File fileInQueue = null;
        while (queue.size() > 0) {
            fileInQueue = queue.poll();
            File[] files = fileInQueue.listFiles();
            for (File eachFile : files) {
                if (eachFile.isFile()) {
                    System.out.println(eachFile.getCanonicalPath());
                } else {
                    System.out.println(eachFile.getCanonicalPath());
                    queue.offer(eachFile);
                }
            }
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值