1 PackageAlgorithms.util;2 3 /******************************************************************************4 * Compilation:javac Directory.java5 * Execution:java Directory directory-name6 * Dependencies:Queue.java Stdout.java7 * 8 * Prints out all of the files in the given directory and any9 * subdirectories in Level-order by using a queue. Also PrintsTen * out their file sizes in bytes. One * A *% java Directory. - * - ******************************************************************************/ the - ImportJava.io.File; - - Importalgorithms. Adt. Queue; + - Public classDirectory { + A Public Static voidMain (string[] args) { atqueue<file> queue =NewQueue<file>(); -File root =NewFile (Args[0]);//root directory - if(!root.exists ()) { -Stdout.println (Args[0] + "does not exist"); - return; - } in - Queue.enqueue (root); to while(!Queue.isempty ()) { +File x =queue.dequeue (); - if(!x.isdirectory ()) { theStdout.println (X.length () + ": \ T" +x); * } $ Else {Panax Notoginsengfile[] Files =x.listfiles (); - for(inti = 0; i < files.length; i++) the Queue.enqueue (Files[i]); + } A } the } + -}
Algorithm Sedgewick Fourth Edition-1th chapter basic -025-using queues to implement directory commands under Unix