Program to Traverse a Directory (all sub-directories and files) in 2 ways | using org.apache.commons.io.FileUtils in java file IO

In this post we’ll write program to Traverse a Directory (all sub-directories and files) in 2 ways | using org.apache.commons.io.FileUtils in java file IO >

  • Program 1 to Traverse a Directory (all sub-directories and files) in java

  • Program 2 to Traverse all files in directory and its sub-directories using org.apache.commons.io.FileUtils's iterateFiles(dir, null, true)



Suppose you have to delete "c:/myDirectory" and your directory structure is like this


Program 1 to Traverse a Directory (all sub-directories and files) in java

java.io.File does not provide any direct method to iterate over Directory

we'll write method traveseSubDirAndFiles() which recursively displays all sub-directories and files present in that directory


import java.io.File;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class TraverseDirectory {
  
   public static void main(String... args) {
          String directoryName = "c:/myDirectory";
          File directoryToBeTraversed = new File(directoryName);
         
          if(directoryToBeTraversed.isDirectory()){ //Ensure it is Directory  
                
                 try {
                       traveseSubDirAndFiles(directoryToBeTraversed);
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
          else{            
                 System.out.println(directoryName +" isn't a Directory");
          }
         
   }
  
  
  
   /*
   *  traveseSubDirAndFiles() method recursively displays all
   *  sub-directories and files present in that directory
   */
    public static void traveseSubDirAndFiles(File dir) throws IOException {     
                
                 System.out.println(dir.getAbsoluteFile());
                
                 //If currently we are on directory, obtain all sub-directories
                 //and files in allSubDirAndFiles[]
                 if(dir.isDirectory()){
                       String allSubDirAndFiles[] = dir.list();
                       //iterate over allSubDirAndFiles[] i.e. iterate
                       //over sub-directories and files
                       for(String filename : allSubDirAndFiles){
                              traveseSubDirAndFiles(new File(dir, filename));
                       }
                 }
                
   }
}
/* OUTPUT
c:\myDirectory
c:\myDirectory\myFile1.txt
c:\myDirectory\myFile2.txt
c:\myDirectory\myFile3.txt
c:\myDirectory\myFile4.txt
c:\myDirectory\sub-directory
c:\myDirectory\sub-directory\sub-directory myFile1.txt
c:\myDirectory\sub-directory\sub-directory myFile2.txt
c:\myDirectory\sub-directory\sub-directory myFile3.txt
c:\myDirectory\sub-directory\sub-directory myFile4.txt
*/



Must read Directory operations - create, identify, delete, copy.

Program 2 to Traverse all files in directory and its sub-directories using org.apache.commons.io.FileUtils's iterateFiles(dir, null, true)

Traverse all files in directory and its sub-directories using  
org.apache.commons.io.FileUtils's iterateFiles(dir, null, true)
first parameter - specify directory
second parameter - specify specific extensions. Eg- you may like to display only .pdf or .docx files
third parameter - search has to recursive or not directory


org.apache.commons.io.FileUtils's iterateFiles(dir, null, true) method can be used to iterate over all files in directory and its sub-directories

keeping third parameter as false won't do recursive search and will iterate over all files in directory and not its sub-directories
org.apache.commons.io.FileUtils's iterateFiles(dir, null, false) method can be used to iterate over all files in directory and its sub-directories


import java.io.File;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
/** JavaMadeSoEasy.com */
public class TraverseDirectory {
  
   public static void main(String... args) {
          String dirName = "c:/myDirectory";
          File dir = new File(dirName);
          Iterator<FileUtils> iterator = FileUtils.iterateFiles(dir, null, true);
          while (iterator.hasNext()) {
                 System.out.println(iterator.next());
          }
   }
  
}
/* OUTPUT
c:\myDirectory\myFile1.txt
c:\myDirectory\myFile2.txt
c:\myDirectory\myFile3.txt
c:\myDirectory\myFile4.txt
c:\myDirectory\sub-directory\sub-directory myFile1.txt
c:\myDirectory\sub-directory\sub-directory myFile2.txt
c:\myDirectory\sub-directory\sub-directory myFile3.txt
c:\myDirectory\sub-directory\sub-directory myFile4.txt
*/

Note : In order to avoid java.io.FileNotFoundException you must use java.io.File's isDirectory() method to ensure that directoryName is a directory. I haven't used it just to keep the code simple.



RELATED LINKS>

Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


2 Program to Delete a Directory


Copy a Directory


Program to Replace all the occurrences of searchWord in the file with replaceWord in java

Write String to file using FileOutputStream in java file IO

Create File using createNewFile() method in java file IO


Difference between FileInputStream and BufferedInputStream in java file IO



No comments:

Post a Comment