zoukankan      html  css  js  c++  java
  • 算法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 }
  • 相关阅读:
    JavaScript笔记三两个
    形式参数分别是基本类型和引用类型的调用
    if (strAreaCode.Find("体检")>=0)
    C++编写DLL文件
    error LNK2001: unresolved external symbol __imp__closesocket@4
    accept函数
    socket编程
    MFC控件使用大全
    DLL导出函数
    LINK : fatal error LNK1104: cannot open file的解决方法
  • 原文地址:https://www.cnblogs.com/shamgod/p/5412012.html
Copyright © 2011-2022 走看看