服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java递归与非递归实现扫描文件夹下所有文件

java递归与非递归实现扫描文件夹下所有文件

2021-04-02 11:17LQ55 Java教程

这篇文章主要为大家详细介绍了java递归与非递归实现扫描文件夹下所有文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

java扫描指定文件夹下面的所有文件,供大家参考,具体内容如下

扫描一个文件夹下面的所有文件,因为文件夹的层数没有限制可能多达几十层几百层,通常会采用两种方式来遍历指定文件夹下面的所有文件。

  • 递归方式
  • 非递归方式(采用队列或者栈实现)

下面我就给出两种方式的实现代码,包括了递归与非递归实现,code如下所示。

java代码:

?
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package q.test.filescanner;
 
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
 
import q.test.filescanner.exception.ScanFilesException;
 
/**
 * @author 邪恶小先生
 */
public class FolderFileScanner {
   
  private static ArrayList<Object> scanFiles = new ArrayList<Object>();
   
  /**linkedList实现**/
  private static LinkedList<File> queueFiles = new LinkedList<File>();
   
   
  /**
   * TODO:递归扫描指定文件夹下面的指定文件
   * @return ArrayList<Object>
   * @author 邪恶小先生(LQ)
   * @time 2017年11月3日
   */
  public static ArrayList<Object> scanFilesWithRecursion(String folderPath) throws ScanFilesException{
    ArrayList<String> dirctorys = new ArrayList<String>();
    File directory = new File(folderPath);
    if(!directory.isDirectory()){
      throw new ScanFilesException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");
    }
    if(directory.isDirectory()){
      File [] filelist = directory.listFiles();
      for(int i = 0; i < filelist.length; i ++){
        /**如果当前是文件夹,进入递归扫描文件夹**/
        if(filelist[i].isDirectory()){
          dirctorys.add(filelist[i].getAbsolutePath());
          /**递归扫描下面的文件夹**/
          scanFilesWithRecursion(filelist[i].getAbsolutePath());
        }
        /**非文件夹**/
        else{
          scanFiles.add(filelist[i].getAbsolutePath());
        }
      }
    }
    return scanFiles;
  }
   
  /**
   *
   * TODO:非递归方式扫描指定文件夹下面的所有文件
   * @return ArrayList<Object>
   * @param folderPath 需要进行文件扫描的文件夹路径
   * @author 邪恶小先生(LQ)
   * @time 2017年11月3日
   */
  public static ArrayList<Object> scanFilesWithNoRecursion(String folderPath) throws ScanFilesException{
    File directory = new File(folderPath);
    if(!directory.isDirectory()){
      throw new ScanFilesException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");
    }
    else{
      //首先将第一层目录扫描一遍
      File [] files = directory.listFiles();
      //遍历扫出的文件数组,如果是文件夹,将其放入到linkedList中稍后处理
      for(int i = 0; i < files.length; i ++){
        if(files[i].isDirectory()){
          queueFiles.add(files[i]);
        }else{
          //暂时将文件名放入scanFiles中
          scanFiles.add(files[i].getAbsolutePath());
        }
      }
       
      //如果linkedList非空遍历linkedList
      while(!queueFiles.isEmpty()){
        //移出linkedList中的第一个
        File headDirectory = queueFiles.removeFirst();
        File [] currentFiles = headDirectory.listFiles();
        for(int j = 0; j < currentFiles.length; j ++){
          if(currentFiles[j].isDirectory()){
            //如果仍然是文件夹,将其放入linkedList中
            queueFiles.add(currentFiles[j]);
          }else{
            scanFiles.add(currentFiles[j].getAbsolutePath());
          }
        }
      }
    }
     
    return scanFiles;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/lq15310444798/article/details/78596482

延伸 · 阅读

精彩推荐