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


     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() + ":	" + 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 }
  • 相关阅读:
    单例模型
    数据库7 索引
    数据库6.高级
    数据库5 不想改
    绑定方法与非绑定方法 反射 内置方法
    组合 封装 多态
    面向对象之继承
    面向过程编程
    logging hashlib 模块
    pickle json xml shelve configparser模块
  • 原文地址:https://www.cnblogs.com/shamgod/p/5412012.html
Copyright © 2020-2023  润新知