CS 1721 Laboratory 9
Using recursion to traverse a directory

Objectives:

Hand-in Requirements;

All projects and laboratories will be submitted electronically through webCT. Zip up your entire project directory to submit as the source. (Right click on the project folder and follow the SentTo link.) There is no output required for this lab as the grader will test the running of your code. Please organize your output neatly so that it is readable and identified with its part in the lab.

Overview:

This recitation exercise explores how to access system directories through the File class that is part of the java.io package. Directories under the Windows and Unix operating systems have a tree structure. That is, a directory is just a file that contain files and other directories. The files and other directories are its children and it is the parent. Showing all of the files in a directory tree (showDirectoryTree) is naturally a recursive process: The project will use the Java File class ( http://java.sun.com/j2se/1.5/docs/api/java/io/File.html) that is part of the java.io package.

Part I: Project setup and experimentation with File objects.

On your own, put the code in a loop so that you can add several files. Experiment with different files and with giving just file names and with giving full path names.

Part II: Directory traversal and display

Note: The array of strings returned by the list method of File just gives filenames. You need to concatenate that file name to the directory path name in order to get the right file when you proceed with the recursive calls.

Part III: Enhancing the directory display (on your own)