一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件

java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件

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

這篇文章主要為大家詳細(xì)介紹了java遞歸與非遞歸實(shí)現(xiàn)掃描文件夾下所有文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

java掃描指定文件夾下面的所有文件,供大家參考,具體內(nèi)容如下

掃描一個(gè)文件夾下面的所有文件,因?yàn)槲募A的層數(shù)沒(méi)有限制可能多達(dá)幾十層幾百層,通常會(huì)采用兩種方式來(lái)遍歷指定文件夾下面的所有文件。

  • 遞歸方式
  • 非遞歸方式(采用隊(duì)列或者棧實(shí)現(xiàn))

下面我就給出兩種方式的實(shí)現(xiàn)代碼,包括了遞歸與非遞歸實(shí)現(xiàn),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實(shí)現(xiàn)**/
  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 ++){
        /**如果當(dāng)前是文件夾,進(jìn)入遞歸掃描文件夾**/
        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 需要進(jìn)行文件掃描的文件夾路徑
   * @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();
      //遍歷掃出的文件數(shù)組,如果是文件夾,將其放入到linkedList中稍后處理
      for(int i = 0; i < files.length; i ++){
        if(files[i].isDirectory()){
          queueFiles.add(files[i]);
        }else{
          //暫時(shí)將文件名放入scanFiles中
          scanFiles.add(files[i].getAbsolutePath());
        }
      }
       
      //如果linkedList非空遍歷linkedList
      while(!queueFiles.isEmpty()){
        //移出linkedList中的第一個(gè)
        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;
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲XXX午休国产熟女屁 | 久草在在线免视频在线观看 | 色综合久久六月婷婷中文字幕 | 免费抽搐一进一出印度 | 国产精品久久久久久影视 | 午夜伦理yy44008影院 | 全色黄大色黄大片爽一次 | 亚洲男人的天堂网站 | 韩国三级2020| 日本公与妇中文在线 | aa一级护士医生毛片 | 国产91免费在线 | 九九99热久久999精品 | 亚洲国产99 | 日本孕妇大胆孕交 | 天天干天天日天天射天天操毛片 | 2022最新国产在线不卡a | 99久久精品国产综合一区 | avtt一区| 扒开女人下面 | 国产欧美一区二区精品性色99 | 喷奶水榨乳ova动漫无修 | 国产区香蕉精品系列在线观看不卡 | 青青热久免费精品视频网站 | 日韩视频免费一区二区三区 | 成人免费高清视频 | 爽新片xxxxxxx| 秋霞一级黄色片 | 丝袜老师好湿好紧我要进去了 | 扒开老师挠尿口到崩溃刑罚 | 全彩调教侵犯h本子全彩妖气he | 免费观看视频在线播放 | 美尻在线| 亚洲精品视频专区 | 草榴色导航 | oneday日本在线观看完整版 | 无限资源在线观看播放 | 欧美亚洲第一区 | 91视在线国内在线播放酒店 | 日韩毛片高清在线看 | 国产一卡2卡3卡四卡精品网 |