import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; import java.util.List; // we don't want java.awt.List /** * FileSystem is a window displays a given directory subtree. */ public class FileSystem extends JFrame { /** * Make a FileSystem window. * @param root Root of directory subtree to display in the new window */ public FileSystem(File root) { super("File System: " + root); // call System.exit() when user closes the window setDefaultCloseOperation(EXIT_ON_CLOSE); // traverse the filesystem recursively, starting at root traverse(root); } // Traverse the filesystem subtree rooted at f. // @effects Currently prints the names of files found to standard output. // Change this to have the appropriate effects on your window. private void traverse(File f) { System.out.println(f.getPath()); if (f.isDirectory()) { String[] children = f.list(); for (int i = 0; i < children.length; ++i) { traverse(new File(f, children[i])); } } } /** * Main method. Makes and displays a FileSystem window. * @param args Command-line arguments: args[0] should be the * pathname to display as the root. If args[0] is omitted, * uses the current directory instead. */ public static void main(String[] args) { // root must be final so that it can be accessed from // the inner Runnable class below final File root = (args.length > 0) ? new File(args[0]) : new File("."); SwingUtilities.invokeLater(new Runnable() { public void run () { // Make and display the FileSystem window. new FileSystem(root).show(); } }); } }