算法1 : 递归

一个简单的递归程序,它读取给定目录下所有文件,然后以树型的方式在屏幕上打印出来.昨天断断续续得花了1个多小时写出来

读取文件

None.gif package  test;
None.gif
import  java.io. * ;
None.gif
import  java.util. * ;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif *   Put files into a tree
ExpandedBlockEnd.gif * 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  ReadFilesAndBuiltTree  dot.gif {
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
static   class  Node dot.gif {
InBlock.gif        
private  Node father;
InBlock.gif        
private  List children;
InBlock.gif        
private  File data;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  Node() dot.gif {
InBlock.gif            children 
=   new  Vector();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  File getData()  dot.gif {
InBlock.gif            
return  data;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   void  addChild(Node c) dot.gif {
InBlock.gif            
this .getChildren().add(c);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   void  setData(File data)  dot.gif {
InBlock.gif            
this .data  =  data;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  List getChildren()  dot.gif {
InBlock.gif            
return  children;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   void  setChildren(List children)  dot.gif {
InBlock.gif            
this .children  =  children;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  Node getFather()  dot.gif {
InBlock.gif            
return  father;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   void  setFather(Node father)  dot.gif {
InBlock.gif            
this .father  =  father;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public   boolean  hasChildren() dot.gif {
InBlock.gif            
return  children.size() > 0 ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  Node getChile( int  index) dot.gif {
InBlock.gif            
return  (Node)children.get(index);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  Node getChild(String fname) dot.gif {
InBlock.gif            Iterator i 
=  getChildren().iterator();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
while (i.hasNext()) dot.gif {
InBlock.gif                Node bn 
=  (Node)i.next();
InBlock.gif                String n 
=  
InBlock.gif                    bn.getData().getName();
InBlock.gif                
if (n.equals(fname)) return  bn;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return   null ;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  List getDirChildren() dot.gif {
InBlock.gif            List c 
=   new  Vector();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for ( int  i  =   0  ; i  < children.size();i ++ ) dot.gif {
InBlock.gif                Node n 
=  (Node)children.get(i);
InBlock.gif                
if (
InBlock.gif                  n.getData().isDirectory()        
InBlock.gif                )c.add(n);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return  c;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
public  List getFileChildren() dot.gif {
InBlock.gif            List c 
=   new  Vector();
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for ( int  i  =   0  ; i  < children.size();i ++ ) dot.gif {
InBlock.gif                Node n 
=  (Node)children.get(i);
InBlock.gif                
if (
InBlock.gif                  
! n.getData().isDirectory()        
InBlock.gif                )c.add(n);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return  c;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private  ReadFilesAndBuiltTree() dot.gif {}
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static  Node getTree(File rootDir) dot.gif {
InBlock.gif        Node r 
=   new  Node();
InBlock.gif        r.setData(rootDir);
InBlock.gif        
return   new  ReadFilesAndBuiltTree().builtTree(r);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private  Node builtTree(Node root) dot.gif {
InBlock.gif        
if ( ! root.getData().isDirectory()) return  root;
InBlock.gif        File f 
=  root.getData();
InBlock.gif        File[] fs 
=  f.listFiles();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for ( int  i  =   0  ; i  <  fs.length ; i ++  ) dot.gif {
InBlock.gif            File ff 
=  fs[i];
InBlock.gif            Node n 
=   new  Node();
InBlock.gif            n.setData(ff);
InBlock.gif            n.setFather(root);
InBlock.gif            root.addChild(builtTree(n));
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return  root;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public   static   void  main(String[] args) throws  Exception dot.gif {
InBlock.gif        Node root 
=  getTree( new  File(ShowFileDir.ROOT_DIR));
InBlock.gif        Node cn 
=  root.getChild( " maps " );
InBlock.gif        System.out.println(cn.getChildren().size());
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedBlockEnd.gif}

None.gif

输出文件
None.gif package  test;
None.gif
import  java.io.File;
None.gif
import  java.util.Iterator;
None.gif
import  java.util.List;
None.gif
None.gif
import  test.ReadFilesAndBuiltTree.Node;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/** */ /**
InBlock.gif *  Print the tree to screen
ExpandedBlockEnd.gif * 
*/

ExpandedBlockStart.gifContractedBlock.gif
public   class  ShowFileDir  dot.gif {
InBlock.gif    
InBlock.gif    
public static String ROOT_DIR = "E:/y音乐";
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public ShowFileDir()dot.gif{
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void printNodes()dot.gif{
InBlock.gif       Node root 
= ReadFilesAndBuiltTree.getTree(new File(ROOT_DIR));
InBlock.gif       printNode(root,
0);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private void printNode(Node root,int depth)dot.gif{
InBlock.gif        
if(!root.hasChildren())return;
InBlock.gif        pd(root.getData().getName(),depth);
InBlock.gif        pindent(
++depth);
InBlock.gif        List l 
= root.getFileChildren();
InBlock.gif        pc(l,depth);
InBlock.gif        l 
= root.getDirChildren();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i = 0 ; i < l.size() ; i++)dot.gif{
InBlock.gif            Node c 
= (Node)l.get(i);
InBlock.gif            printNode(c,depth);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
// Screen methods    
ExpandedSubBlockStart.gifContractedSubBlock.gif
    private void pc(List l,int count)dot.gif{
InBlock.gif        Iterator i 
= l.iterator();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while(i.hasNext())dot.gif{
InBlock.gif            Node n 
= (Node)i.next();
InBlock.gif            pp(count);
InBlock.gif            p(
"+"+n.getData().getName()+"\n");
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值