/*******************************************************************************
Author:                         Rebecca Hasti, hasti@cs.wisc.edu
                                        copyright 2000, all rights reserved
Compiler:                       Metrowerks CodeWarrior (JDK 1.2)
Platform:                       Windows NT 4.0 (or Windows 95)
*******************************************************************************/

import java.io.*;

public class Directory {

	public static void main(String args[]) throws IOException {
	
		BufferedReader stdin = new BufferedReader(
		                           new InputStreamReader(System.in));
		String inputStr;
		File directory;
		boolean badDir, again;
		
		do { // outer loop that repeats the prompting and printing
	
			// the following loop prompts the user for a directory and repeats
			// until the user enters a valid directory name
			
			do {
        		System.out.print("Please enter a directory: ");
        		inputStr = stdin.readLine();
        		directory = new File(inputStr);
        	
        		if (!directory.isDirectory()) {
        			System.out.println(inputStr + " is not a directory");
        			badDir = true;
        		}
        		
        		else 
        			badDir = false;
       		} while (badDir);
       
       		// print out the contents of the directory
       		
       		System.out.print("\nContents of " + inputStr + "  ");
       		String[] fileNames = directory.list();
       		System.out.println(fileNames.length + " items");
       		
       		// for each item in the directory list
       		for (int i = 0; i < fileNames.length; i++) {
       		
       			// if the item begins with a ., don't print it out
       			if (fileNames[i].charAt(0) == '.')
       				continue;
       				
       			System.out.print('\t' + fileNames[i]);
       			File currFile = new File(inputStr, fileNames[i]);
       			
       			// if the item is a directory, add a slash and the number of
       			// items in the directory
       			if (currFile.isDirectory()) {
       				System.out.print("\\  " + currFile.list().length +
       			                     " items");
       			}
       			
       			System.out.println();
       		}
       
       		// ask the user if s/he wants to go again
       		System.out.print("\nAgain? ");
       		inputStr = stdin.readLine();
    	} while (inputStr.charAt(0) == 'y' || inputStr.charAt(0) == 'Y');
	}
}
