zoukankan      html  css  js  c++  java
  • Algs4-1.3.43文件列表

     1.3.43文件列表。文件夹就是一列文件和文件夹的列表。编写一个程序,从命令行接受一个文件夹名作为参数,打印出访文件夹下的所有文件并用递归的方式在所有子文件夹的名下(缩进)列出其下的所有文件。提示:使用队列,并参考java.io.File。
    答:
    图片
    import java.io.File;
    public class test
    {
        private class MyFile
        {
            String name;
            int level;
        }
        public static void main(String[] args)
        {
           String dirName=args[0];
           File f=new File(dirName);
           Queue<MyFile> q=new Queue<MyFile>();
           int level=0;
           test mytest=new test();
           mytest.ShowAllDirAndFile(f,q,level);
            for(MyFile i:q)
                StdOut.printf("%"+3*i.level+"s%s ","",i.name);
         }//end main
       
        public  void ShowAllDirAndFile(File f,Queue<MyFile> q,int level)
        {
            if(f==null) return;
             level++;
            if(f.isDirectory())
            {
                MyFile myfile=new MyFile();
                myfile.name=f.getName().toString();
                myfile.level=level;
                q.enqueue(myfile);
                File[] files=f.listFiles();
               if(files.length>0)
                for(File file:files)
                   ShowAllDirAndFile(file,q,level);
            }
            else
            {
                MyFile myfile=new MyFile();
                myfile.name=f.getName().toString();
                myfile.level=level;
                q.enqueue(myfile);
            }
        }
      }//end class
  • 相关阅读:
    (15)树莓派系统安装和备份
    (0-0) 树莓派学习资料
    (14)树莓派
    (0-1) 树莓派常用软件及服务
    (13)flask搭建服务器
    (12)树莓派串口通信
    OpenCV 学习笔记(0)两幅图像标定配准
    OpenCV 学习笔记(9)RGB转换成灰度图像的一个常用公式Gray = R*0.299 + G*0.587 + B*0.114
    OpenCV 学习笔记(8)彩色图像RGB通道的分离、合并与显示
    Arduino OV7670 live image over USB to PC
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9854329.html
Copyright © 2011-2022 走看看