Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 477|回复: 0

[算法学习]递归--树型的方式输出目录下的文件

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:57:39 | 显示全部楼层 |阅读模式

    1. 一个简单的递归程序,它读取给定目录下所有文件,然后以树型的方式在屏幕上打印出来
    2. 来源:http://www.blogjava.net/zqc/archive/2006/04/23/42581.aspx
    复制代码
    1. 一、读取文件
    2. package test;
    3. import  java.io. * ;
    4. import  java.util. * ;
    5. /**
    6. *   Put files into a tree
    7. **/
    8.   public   class  ReadFilesAndBuiltTree   {
    9.    
    10.      static   class  Node  {
    11.          private  Node father;
    12.          private  List children;
    13.          private  File data;
    14.          public  Node()  {
    15.             children  =   new  Vector();
    16.         }

    17.      public  File getData()   {
    18.              return  data;
    19.         }

    20.           public   void  addChild(Node c)  {
    21.              this .getChildren().add(c);
    22.         }
    23.           public   void  setData(File data)   {
    24.              this .data  =  data;
    25.         }
    26.           public  List getChildren()   {
    27.              return  children;
    28.         }
    29.           public   void  setChildren(List children)   {
    30.              this .children  =  children;
    31.         }
    32.           public  Node getFather()   {
    33.              return  father;
    34.         }
    35.           public   void  setFather(Node father)   {
    36.              this .father  =  father;
    37.         }
    38.           public   boolean  hasChildren()  {
    39.              return  children.size() > 0 ;
    40.         }
    41.           public  Node getChile( int  index)  {
    42.              return  (Node)children.get(index);
    43.         }
    44.         
    复制代码

      
    1.   public  Node getChild(String fname)  {
    2.             Iterator i  =  getChildren().iterator();
    3.              while (i.hasNext())  {
    4.                 Node bn  =  (Node)i.next();
    5.                 String n  =  
    6.                     bn.getData().getName();
    7.                  if (n.equals(fname)) return  bn;
    8.             }
    9.              return   null ;
    10.         }
    11.           public  List getDirChildren()  {
    12.             List c  =   new  Vector();
    13.              for ( int  i  =   0  ; i  < children.size();i ++ )  {
    14.                 Node n  =  (Node)children.get(i);
    15.                  if (
    16.                   n.getData().isDirectory()        
    17.                 )c.add(n);
    18.             }
    19.              return  c;
    20.         }
    21.           public  List getFileChildren()  {
    22.             List c  =   new  Vector();
    23.              for ( int  i  =   0  ; i  < children.size();i ++ )  {
    24.                 Node n  =  (Node)children.get(i);
    25.                  if (
    26.                    ! n.getData().isDirectory()        
    27.                 )c.add(n);
    28.             }
    29.              return  c;
    30.         }
    31.     }
    32.    
    33.      private  ReadFilesAndBuiltTree()  {}
    34.    
    35.      public   static  Node getTree(File rootDir)  {
    36.         Node r  =   new  Node();
    37.         r.setData(rootDir);
    38.          return   new  ReadFilesAndBuiltTree().builtTree(r);
    39.     }
    40.    
    41.      private  Node builtTree(Node root)  {
    42.          if ( ! root.getData().isDirectory()) return  root;
    43.         File f  =  root.getData();
    44.         File[] fs  =  f.listFiles();
    45.          for ( int  i  =   0  ; i  <  fs.length ; i ++  )  {
    46.             File ff  =  fs[i];
    47.             Node n  =   new  Node();
    48.             n.setData(ff);
    49.             n.setFather(root);
    50.             root.addChild(builtTree(n));
    51.         }
    52.          return  root;
    53.     }
    54.    
    55.      public   static   void  main(String[] args) throws  Exception  {
    56.         Node root  =  getTree( new  File(ShowFileDir.ROOT_DIR));
    57.         Node cn  =  root.getChild( " maps " );
    58.         System.out.println(cn.getChildren().size());
    59.     }
    60.    
    61. }
    复制代码
    二、输出
    1. package test;
    2. import java.io.File;
    3. import java.util.Iterator;
    4. import java.util.List;
    5. import test.ReadFilesAndBuiltTree.Node;
    6. /**
    7. *  Print the tree to screen
    8. * */
    9. public class ShowFileDir {
    10.    
    11.     public static String ROOT_DIR = "c:/java";
    12.    
    13.     public ShowFileDir(){
    14.     }
    15.    
    16.     public void printNodes(){
    17.        Node root = ReadFilesAndBuiltTree.getTree(new File(ROOT_DIR));
    18.        printNode(root,0);
    19.     }
    20.    
    21.     private void printNode(Node root,int depth){
    22.         if(!root.hasChildren())return;
    23.         pd(root.getData().getName(),depth);
    24.         pindent(++depth);
    25.         List l = root.getFileChildren();
    26.         pc(l,depth);
    27.         l = root.getDirChildren();
    28.         for(int i = 0 ; i < l.size() ; i++){
    29.             Node c = (Node)l.get(i);
    30.             printNode(c,depth);
    31.         }
    32.     }
    33.    
    34. // Screen methods   
    35.     private void pc(List l,int count){
    36.         Iterator i = l.iterator();
    37.         while(i.hasNext()){
    38.             Node n = (Node)i.next();
    39.             pp(count);
    40.             p("+"+n.getData().getName()+"
    41. ");
    42.         }
    43.     }
    44.    
    45.     private void pd(String t,int depth){
    46.         String spc = "";
    47.         for(int i = 0; i< depth; i++)spc+="-";
    48.         p(spc+"+"+t+"
    49. ");
    50.     }
    51.    
    52.     private void pindent(int depth){
    53.         String spc = "";
    54.         for(int i = 0; i< depth; i++)spc+=" ";
    55.         System.out.println(spc+"|");
    56.     }
    57.    
    58.     private void pp(int count){
    59.         for(int i = 0 ; i < count ; i++)p(" ");
    60.     }
    61.    
    62.     private void p(String s){System.out.print(s);}
    63.    
    64.     public static void main(String[] args)throws Exception{
    65.         ShowFileDir sfd = new ShowFileDir();
    66.         sfd.printNodes();
    67.     }
    68. }
    复制代码
      运行结果:

    1. C:java>java  test.ShowFileDir
    2. +java
    3. |
    4. +app.bat
    5. +GetURL.java
    6. +jsf.bat
    7. +Exec.java
    8. +Exec.class
    9. +GetURL.class
    10. -+jar
    11.   |
    12.   +commons-lang.jar
    13.   +jcs-1.0-dev.jar
    14.   +jdom.jar
    15.   +commons-beanutils.jar
    16.   +commons-collections.jar
    17.   +commons-logging.jar
    18.   +bot.jar
    19.   +jaxen-core.jar
    20.   +saxpath.jar
    21.   +jaxen-jdom.jar
    22.   +GetURL.java
    23. -+test
    24.   |
    25.   +ReadFilesAndBuiltTree.java
    26.   +ShowFileDir.java
    27.   +ReadFilesAndBuiltTree$Node.class
    28.   +ReadFilesAndBuiltTree.class
    29.   +ShowFileDir.class
    30. C:java>
    复制代码


      
      
       
       

         
       

         
       
      


    源码下载:http://file.javaxxz.com/2014/10/28/235739453.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 09:31 , Processed in 0.389507 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表