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

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