001package algs13;
002import java.io.File;
003import stdlib.*;
004/* ***********************************************************************
005 *  Compilation:  javac Directory.java
006 *  Execution:    java Directory directory-name
007 *  Dependencies: Queue.java
008 *
009 *  Prints out all of the files in the given directory and any
010 *  subdirectories in level-order by using a queue. Also prints
011 *  out their file sizes in bytes.
012 *
013 *  % java Directory .
014 *
015 *************************************************************************/
016
017public class XDirectory {
018
019        public static void main(String[] args) {
020                Queue<File> q = new Queue<>();
021                File root = new File(args[0]);     // root directory
022                q.enqueue(root);
023                while (!q.isEmpty()) {
024                        File directory = q.dequeue();
025                        File[] files = directory.listFiles();
026                        for (File file : files) {
027                                if (file.isDirectory()) q.enqueue(file);
028                                else StdOut.println(file.length() + ":\t" + file);
029                        }
030                }
031        }
032
033}