DirectoryR.java


Below is the syntax highlighted version of DirectoryR.java from §4.3 Stacks and Queues.


/******************************************************************************
 *  Compilation:  javac DirectoryR.java
 *  Execution:    java DirectoryR directory-name
 *  
 *  Prints out all of the files in the given directory and any
 *  subdirectories. Also prints out their file sizes in bytes.
 *
 ******************************************************************************/

import java.io.File;

public class DirectoryR { 

    public static void list(File directory) {
        File[] files = directory.listFiles();
        if (files == null) return;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) list(files[i]);
            else StdOut.println(files[i].length() + ": " + files[i]);
        }
    }

    public static void main(String[] args) {
        File directory = new File(args[0]);
        list(directory);
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.