Java code for a level order directory traversal with a custom list

Home| Recursive traversal| Level order traversal| Level order traversal with custom list| Traversal with walkFileTree| Traversal with walk
/******************************************************************************/
/*                                                                            */
/*                                                      FILE: CustomList.java */
/*                                                                            */
/*  Level order directory traversal in Java                                   */
/*  =======================================                                   */
/*                                                                            */
/*  V1.00   19-JUN-2009   Te          https://www.heimetli.ch                 */
/*                                                                            */
/******************************************************************************/

import java.io.* ;

/**
 * A custom class for a linked list of File objects.
 *
 * For production code, you are better off by using the LinkedList class in the
 * java.utils package. Look at the StandardList example for this variant.
 */
class FileList
{
   // First node in list
   private Node first ;

   // Last node in the list
   private Node last ;

   /**
    * Sets up an empty list
    */
   public FileList()
   {
      first = null ;
      last  = null ;
   }

   /**
    * Adds a node to the list.
    * @param file The file object for the new node
    */
   public void add( File file )
   {
      Node node = new Node( file ) ;

      if( first == null )
         first = node ;
      else
	 last.next = node ;
 
      last = node ;
   }

   /**
    * Returns the state of the list.
    * @return true if the list is empty
    */
   public boolean empty()
   {
      return (first == null) ;
   }

   /**
    * Removes a node from the list and returns the File object from the node.
    * @return The File object
    */
   public File remove()
   {
      if( first == null )
         throw new IllegalStateException( "List empty" ) ;
 
      File result = first.file ;
 
      first = first.next ;
 
      return result ;
   }

   /**
    * Private node class for the FileList
    */
   private class Node
   {
      // Reference to the next node
      public Node next ;

      // The content of the node
      public File file ;

      /**
       * Constructs a new Node object.
       * @param file the content of the node
       */
      public Node( File file )
      {
          this.file = file ;
          next      = null ;
      }
   }
}

/**
 * This class implements a level order traversal of a directory tree.
 * It uses the FileList above to store the Files to be processed.
 */
public class CustomList
{
   // The list of Files to be processed
   private FileList list ;

   /**
    * Initializes the list
    */
   public CustomList()
   {
      list = new FileList() ;
   }

   /**
    * Traverses the directory tree in level order (breadth first)
    * @param file the first element to be processed
    */
   public void traverse( File file )
   {
      // Add the element to the list
      list.add( file ) ;

      // Loop until the list is empty
      while( !list.empty() )
      {
         // Remove the first element from the list
         file = list.remove() ;

         // Print the name of the entry
         System.out.println( file ) ;

         // Check if it is a directory
         if( file.isDirectory() )
         {
            // Get a list of all the entries in the directory
            String entries[] = file.list() ;

            // Ensure that the list is not null
            if( entries != null )
            {
               // Loop over all the entries
               for( String entry : entries )
	       {
		  // Add them to the list
                  list.add( new File(file,entry) ) ;
	       }
            }
	 }
      }
   }

   /**
    * The program starts here.
    * @param args The arguments from the command line
    */
   public static void main( String args[] )
   {
      // Create an object of this class
      CustomList cl = new CustomList() ;

      if( args.length == 0 )
      {
         // If there are no arguments, traverse the current directory
         cl.traverse( new File(".") ) ;
      }
      else
      {
         // Else process every argument sequentially
         for( String arg : args )
         {
            cl.traverse( new File(arg) ) ;
         }
      }
   }
}
Download Source