import java.io.*;
import java.util.*;
/*******************************************************************************
* Recursively list all files and directories within a given directory   
* If directory not supplied on command line, use current directory 
*******************************************************************************/
public class ListDir
{
    static int origLevel;                                       //static field

    public static void main(String[] args)
    {
        String dirname = args.length > 0 ? args[0] : ".";
        File f = new File(dirname);

        origLevel = f.getAbsolutePath().split("/").length;      //number of "/" in the directory name

        if (f.isDirectory())
        {
            ListDir x = new ListDir(f);                         //instantiate a new ListDir object 
        }                                                       //or simply -->  new ListDir(f)
        else
            System.out.println("file is not a directory: " + dirname);
    }

    /********************************************************************************
    * ListDir constructor - takes a File Object which should be a directory
    *                       and lists all the files within that directory 
    ********************************************************************************/     
    public ListDir(File dir)                                        //constructor
    {
       String spacer = "";                                          //will be used for leading spaces

       try {
            String dirname = dir.getAbsolutePath();
            int level = dirname.split("/").length - origLevel;      //compute the depth of the directory        
            for (int i=0; i<level; i++)                             //add spaces
                spacer += "    ";

            System.out.println();
            System.out.println(spacer + "DIRECTORY: " + dirname );
            System.out.println(spacer + "---------------------------------------------");                   

            String[ ] files = dir.list();                                   //get list of all files
            Arrays.sort(files);                                             //sort that list

            for (int i=0; i < files.length; i++)                            //loop for all files and dirs
            {
                String filename = dirname + "/" + files[i];                 //concatenate the file to directory
                File f = new File(filename);                                //create a File object
                
                String fname = files[i];

                long numOfMilSec = f.lastModified();                        //number of milisec since 1/1/1970
                Date modDate     = new Date(numOfMilSec);                   //create a Date object            

                if (f.isFile())                                             
                    System.out.printf(spacer + "[file] %-26s", fname);      //left justified 26 char 
                if (f.isDirectory())                                        
                    System.out.printf(spacer + "{DIR:} %-26s", fname);       

                System.out.printf("%7d bytes  %s \n", f.length(), modDate); 
            }

            for (int i=0; i < files.length; i++)                            //loop for directories only
            {
                String filename = dirname + "/" + files[i];                 //concatenate the file to directory
                File f = new File(filename);                                //create a File object
                 
                if (f.isDirectory())                                        //if it is a directory
                {
                     ListDir x = new ListDir(f);                            //recursive call 
                }
            }
        }
        catch (Exception e)                                 
        {
            System.out.println("Cannot Access - Skipping - " + e);
        }                                                                                                   
    }
}