标签:
1 package algorithms.util; 2 3 /****************************************************************************** 4 * Compilation: javac Directory.java 5 * Execution: java Directory directory-name 6 * Dependencies: Queue.java StdOut.java 7 * 8 * Prints out all of the files in the given directory and any 9 * subdirectories in level-order by using a queue. Also prints 10 * out their file sizes in bytes. 11 * 12 * % java Directory . 13 * 14 ******************************************************************************/ 15 16 import java.io.File; 17 18 import algorithms.ADT.Queue; 19 20 public class Directory { 21 22 public static void main(String[] args) { 23 Queue<File> queue = new Queue<File>(); 24 File root = new File(args[0]); // root directory 25 if (!root.exists()) { 26 StdOut.println(args[0] + " does not exist"); 27 return; 28 } 29 30 queue.enqueue(root); 31 while (!queue.isEmpty()) { 32 File x = queue.dequeue(); 33 if (!x.isDirectory()) { 34 StdOut.println(x.length() + ":\t" + x); 35 } 36 else { 37 File[] files = x.listFiles(); 38 for (int i = 0; i < files.length; i++) 39 queue.enqueue(files[i]); 40 } 41 } 42 } 43 44 }
算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令
标签:
原文地址:http://www.cnblogs.com/shamgod/p/5412012.html