官术网_书友最值得收藏!

Retrieving all filenames from hierarchical directories using Java

This recipe (and the following) is for the data scientist who wants to retrieve the file paths and names (for some future analysis) from a complex directory structure that contains numerous directories and files inside a root directory.

Getting ready

In order to perform this recipe, we will require the following:

  1. Create directories within directories (as many layers as you want).
  2. Create text files in some of these directories while leaving some directories empty for more excitement.

How to do it...

  1. We are going to create a static method that takes a File argument, which is eventually the root directory or the directory to start with. The method will return a set of files that are found within this root directory (and in all other subsequent directories):
            public static Set<File> listFiles(File rootDir) {  
    
  2. First, create a HashSet that will contain the file information:
            Set<File> fileSet = new HashSet<File>(); 
    
  3. Once the HashSet is created, we need to check whether the root directory or the directories within it are null. For such cases, we do not need to proceed further:
            if (rootDir == null || rootDir.listFiles() == null){ 
                         return fileSet; 
               } 
    
  4. We consider one directory (or file) from the root directory at a time and check whether we are dealing with a file or with a directory. In the case of a file, we add that to our HashSet. In the case of a directory, we recursively call this method again by sending the path and name of the directory:
            for (File fileOrDir : rootDir.listFiles()) { 
                         if (fileOrDir.isFile()){ 
                           fileSet.add(fileOrDir); 
                         } 
                         else{ 
                           fileSet.addAll(listFiles(fileOrDir)); 
                         } 
                 } 
    
  5. Finally, we return the HashSet to the caller of this method:
           return fileSet; 
              } 
    

    The complete method, with the class and the driver method to run it, is as follows:

    import java.io.File; 
    import java.util.HashSet; 
    import java.util.Set; 
     
    public class TestRecursiveDirectoryTraversal { 
       public static void main(String[] args){ 
          System.out.println(listFiles(new File("Path for root 
              directory")).size()); 
       } 
        
       public static Set<File> listFiles(File rootDir) { 
           Set<File> fileSet = new HashSet<File>(); 
           if(rootDir == null || rootDir.listFiles()==null){ 
               return fileSet; 
           } 
           for (File fileOrDir : rootDir.listFiles()) { 
                 if (fileOrDir.isFile()){ 
                   fileSet.add(fileOrDir); 
                 } 
                 else{ 
                   fileSet.addAll(listFiles(fileOrDir)); 
                 } 
         } 
     
           return fileSet; 
       } 
    } 
    
Note

Note the use of HashSet to store the paths and names of the files. This means that we will not have any duplicates since the Set data structures in Java do not contain duplicate entries.

主站蜘蛛池模板: 启东市| 辽阳市| 内丘县| 从江县| 和平县| 靖江市| 三都| 红原县| 禄劝| 南昌市| 曲靖市| 绥中县| 黎平县| 油尖旺区| 金溪县| 灵丘县| 德庆县| 泾阳县| 辛集市| 枣庄市| 汉川市| 双柏县| 广灵县| 玛沁县| 米泉市| 惠来县| 通江县| 曲靖市| 凌云县| 雷州市| 慈溪市| 资溪县| 石柱| 高台县| 临高县| 嘉荫县| 宾川县| 龙泉市| 鹤岗市| 南投市| 富宁县|