• java之递归搜索本地磁盘


    递归是什么

    递归是指函数的定义中函数使用自身方法。即自己调用自己

    例如下面:

    // 求5的阶乘5!
        int sum = 1;
        for (int i = 1; i <= 5; i++) {
          sum = sum * i;
        }
        System.out.println(sum);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    我们通常会用到for循环,而在使用递归的时候,我们就可以很方便的写出结果

    例如

    // 求5!
      public static int recurve(int i) {
        if (i <= 1) {
          return 1;
        }
    
        return i * recurve(i - 1);
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    既然可以用for循环来求出结果,那么递归有什么用呢

    当然是简化重复代码的操作次数啦

    面对这种简单的代码,可能还看不出递归的优点,那我们换一个代码看看!

    进入今天的主题《递归遍历本地文件》

    遍历文件,我们首要的是需要知道Java中获取本地磁盘

    import java.io.File;
    
    • 1

    File中有一个静态方法可以获取到本地磁盘

    File.listRoots();
    
    
    • 1
    • 2

    他返回了本地有多少个磁盘

    知道了这个,我们就可以开始扫描操作了

    package com.demo.DeepFile;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class DeepFile {
      private File[] RootList = File.listRoots();
      private ArrayList<String> dirname = new ArrayList<>();
      private ArrayList<String> filename = new ArrayList<>();
    
      public DeepFile() {
    
      }
    
      public File[] getRootList() {
        return this.RootList;
      }
    
      public void startDeepFile(File[] list) {
        for (File parentRoot : list) {
          if (parentRoot.listFiles() == null) {
            this.filename.add(parentRoot.getName());
            continue;
          }
          for (File children : parentRoot.listFiles()) {
            if (children.listFiles() == null) {
              this.filename.add(children.getName());
              continue;
            }
            if (children.isDirectory()) {
              this.dirname.add(children.getPath());
              try {
                startDeepFile(children.listFiles());
              } catch (NullPointerException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }
    
      // 将文件写入到文本
      public boolean writePath() {
        try {
          BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
          for (String dir : this.dirname) {
            bw.write(dir);
            bw.newLine();
            bw.flush();
          }
          BufferedWriter fbw = new BufferedWriter(new FileWriter("b.txt"));
          for (String file : this.filename) {
            fbw.write(file);
            fbw.newLine();
            fbw.flush();
          }
          bw.close();
          fbw.close();
        } catch (IOException IOE) {
          IOE.printStackTrace();
        }
    
        return true;
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    遇到Bug需要帮助,
    欢迎加wx:
    xmzl1988
    备注"csdn博客“
    温馨提示此为有偿服务;

  • 相关阅读:
    抖音推荐算法的底层逻辑,互动率包含什么指标?为什么它这么重要?
    数额结构(6.1~6.8)
    深度学习(PyTorch)——循环神经网络(RNN)进阶篇
    微软宣布 S2C2F 已被 OpenSSF 采用
    华为mate60的发布代表着什么?有什么意义?
    第17章 标准库特殊设施【C++】
    二维码智慧门牌管理系统:提升小区管理的智能化水平
    百度无人驾驶商业化已“上路”
    flex布局(学习笔记)
    Axure学习之路01——元件介绍
  • 原文地址:https://blog.csdn.net/weixin_42575720/article/details/126250621