码迷,mamicode.com
首页 > 编程语言 > 详细

算法Sedgewick第四版-第1章基础-025-用队列实现unix下的Directory命令

时间:2016-04-20 13:11:39      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

 

 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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!