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
  • 相关阅读:
    java RSA加密解密
    spring boot 错误处理机制
    Redis 服务常见的几个错误解决方案
    Nginx目录遍历功能时间相差8小时
    翻过大山越过海洋看到了什么
    【分享】分层明确高度定制化的 Python Flask MVC
    编程浪子客服系统开源啦
    快速搭建一个直播Demo
    免费为网站加上HTTPS
    Mac 下安装Fiddler抓包工具
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9854329.html
Copyright © 2011-2022 走看看