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

Retrieving all filenames from hierarchical directories using Apache Commons IO

Listing of file names in hierarchical directories can be done recursively as demonstrated in the previous recipe. However, this can be done in a much easier and convenient way and with less coding using the Apache Commons IO library.

Getting ready

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

  1. In this recipe, we will be using a Java library from Apache named Commons IO. Throughout the book, we will be using version 2.5. Download the JAR file of your choice from here: https://commons.apache.org/proper/commons-io/download_io.cgi
  2. Include the JAR file in your project an external JAR in Eclipse.

How to do it...

  1. Create a method that takes the root directory in the hierarchy of directories as input:
            public void listFiles(String rootDir){ 
    
  2. Create a file object with the root directory name:
            File dir = new File(rootDir); 
    
  3. The FileUtils class of the Apache Commons library contains a method named listFiles(). Use this method to retrieve all the file names, and put the names in a list variable with <File> generics. Use TrueFileFilter.INSTANCE to match all directories:
            List<File> files = (List<File>) FileUtils.listFiles(dir,   
              TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); 
    
  4. The file names can be displayed on the standard output as follows. As we now have the names in a list, we have a means to process the data in these files further:
            for (File file : files) { 
               System.out.println("file: " + file.getAbsolutePath()); 
            } 
    
  5. Close the method:
           } 
    

    The method in this recipe, the class for it, and the driver method to run it are as follows:

    import java.io.File; 
    import java.util.List; 
    import org.apache.commons.io.FileUtils; 
    import org.apache.commons.io.filefilter.TrueFileFilter; 
     
    public class FileListing{ 
       public static void main (String[] args){ 
          FileListing fileListing = new FileListing(); 
          fileListing.listFiles("Path for the root directory here"); 
       } 
       public void listFiles(String rootDir){ 
          File dir = new File(rootDir); 
     
          List<File> files = (List<File>) FileUtils.listFiles(dir,  
            TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE); 
          for (File file : files) { 
             System.out.println("file: " + file.getAbsolutePath()); 
          } 
       } 
    
Tip

If you want to list files with some particular extensions, there is a method in Apache Commons library called listFiles, too. However, the parameters are different; the method takes three parameters, namely, file directory, String[] extensions, boolean recursive. Another interesting method in this library is listFilesAndDirs (File directory, IOFileFilter fileFilter, IOFileFilter dirFilter) if someone is interested in listing not only files but also directories. Detailed information can be found at https://commons.apache.org/proper/commons-io/javadocs/.

主站蜘蛛池模板: 开封县| 耿马| 沙田区| 常州市| 郎溪县| 平顶山市| 琼结县| 泸州市| 湘西| 民勤县| 镇江市| 常德市| 南华县| 嘉义县| 洱源县| 石狮市| 鞍山市| 宜兰县| 林芝县| 奉新县| 华池县| 武隆县| 北票市| 新兴县| 台东市| 拜泉县| 曲阳县| 青川县| 伊宁市| 济南市| 临高县| 五台县| 平潭县| 会理县| 体育| 泰州市| 蕲春县| 罗平县| 专栏| 保定市| 江阴市|