Book Image

Java Data Science Cookbook

By : Rushdi Shams
Book Image

Java Data Science Cookbook

By: Rushdi Shams

Overview of this book

If you are looking to build data science models that are good for production, Java has come to the rescue. With the aid of strong libraries such as MLlib, Weka, DL4j, and more, you can efficiently perform all the data science tasks you need to. This unique book provides modern recipes to solve your common and not-so-common data science-related problems. We start with recipes to help you obtain, clean, index, and search data. Then you will learn a variety of techniques to analyze, learn from, and retrieve information from data. You will also understand how to handle big data, learn deeply from data, and visualize data. Finally, you will work through unique recipes that solve your problems while taking data science to production, writing distributed data science applications, and much more - things that will come in handy at work.
Table of Contents (16 chapters)
Java Data Science Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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