Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Java Data Science Cookbook

You're reading from   Java Data Science Cookbook Explore the power of MLlib, DL4j, Weka, and more

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787122536
Length 372 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Rushdi Shams Rushdi Shams
Author Profile Icon Rushdi Shams
Rushdi Shams
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Obtaining and Cleaning Data FREE CHAPTER 2. Indexing and Searching Data 3. Analyzing Data Statistically 4. Learning from Data - Part 1 5. Learning from Data - Part 2 6. Retrieving Information from Text Data 7. Handling Big Data 8. Learn Deeply from Data 9. Visualizing Data

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/.

You have been reading a chapter from
Java Data Science Cookbook
Published in: Mar 2017
Publisher: Packt
ISBN-13: 9781787122536
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image