/home/wpollock1/public_html/AJava/DirList.java

// Directory listing program.  If no directories are listed on the
// command line then a list for the user's home directory is made.
// For now the output is plain text sent to stdout.  For each directoy
// all files within are listed.  Subdirectories are listed recursively.
// Todo:  Sort files in list, show complete (or connonical) name for
// tops, show output in a swing JTree.
//
// Written 2001 by Wayne Pollock, Tampa Florida USA.

import java.io.*;

public class DirList
{
   private static String [] tops; // The top level dirs/files to list

   public static void main ( String [] args )  throws IOException
   {  if ( args.length == 0 )
      {  tops = new String[1];
         tops[0] = System.getProperty( "user.dir" );
      } else
      {  tops = args;
      }
      StringBuffer indent = new StringBuffer();
      for ( int i = 0; i < tops.length; ++i )
         list( new File( tops[i] ), indent );
   }

   static void list ( File f, StringBuffer indent ) throws IOException
   {  if ( ! fileExists( f ) )   return;
      System.out.println( indent + f.getName() );
      if ( f.isFile() )   return;

      // Must be a directory, so process each file/dir in it:
      indent.append( "   " );  // indent one level
      File [] files = f.listFiles();
      for ( int i = 0; i < files.length; ++i )
      {  if ( ! fileExists( files[i] ) )   continue;
         if ( files[i].isDirectory() )
            list( files[i], indent );  // recursively process subdir
         else
            System.out.println( indent + files[i].getName() );
      }
   }

   static boolean fileExists ( File f )
   {  if ( ! f.exists() )
      {  System.out.println( "DirList: \"" + f.getName() +
            "\" does not exist." );
         return false;
      }
      return true;
   }
}