FileIterator

我们在开发中,经常需要遍历一个目录下的所有文件,常用的办法就是使用一个函数递归遍历是常用的办法。例如:
public   static   void  iterateFile(File file) {
    
if  (file.isDirectory()) {
        
if  (file.getName().startsWith( " . " ))  return ;

        
for  (File item : file.listFiles()) {
            iterateFile(item);
        }
        
return ;
    }

    
//  do something dot.gif
}

但是递归函数的缺点就是扩展不方便,当然你对这个函数加入一个参数FileHandler,这样扩展性稍好一些,但是仍然不够好,比如说,不能根据遍历的需要中途停止遍历,加入Filter等等。我实现了一个FileIterator,使得遍历一个目录下的文件如何遍历一个集合中的元素一般操作。

废话少说,代码如下:

package  net.wenshao;

import  java.io.File;
import  java.util.Iterator;
import  java.util.NoSuchElementException;

public   class  FileIterator  implements  Iterator < File >  {
    
private   static   class  State {
        
final  State parent;
        
final  File[] files;

        
int  index  =   0 ;

        
public  State(State parent, File dir) {
            
this .parent  =  parent;
            files 
=  dir.listFiles();
        }
    }

    
private  File current;

    
private  State state;

    
public  FileIterator(File file) {
        
if  (file.isDirectory()) {
            state 
=   new  State( null , file);
            nextInternal();
        } 
else  {
            
this .current  =  file;
            state 
=   null ;
        }
    }

    @Override
    
public   boolean  hasNext() {
        
return  current  !=   null ;
    }

    @Override
    
public  File next() {
        File rtValue 
=  current;

        
if  (rtValue  ==   null throw   new  NoSuchElementException();

        nextInternal();

        
return  rtValue;
    }

    
private   void  nextInternal() {
        current 
=   null ;

        
if  ( this .state  ==   null return ;

        
for  (;;) {
            
if  (state.index  >=  state.files.length) {
                state 
=  state.parent;
                
if  (state  ==   null return ;

                state.index
++ ;
                
continue ;
            }

            File file 
=  state.files[state.index];
            
            
//  可以在此处加入Filters处理代码
            
            
if  (file.isDirectory()) {
                state 
=   new  State(state, file);
                
continue ;
            }

            current 
=  file;
            state.index
++ ;
            
break ;
        }
    }

    @Override
    
public   void  remove() {
        
throw   new  UnsupportedOperationException();
    }
}

使用FileIterator的例子:
None.gif File dir  =   new  File( " /home/wenshao/workspace " );
None.gif
None.gifIterator
< File >  iter  =   new  FileIterator(dir);
ExpandedBlockStart.gifContractedBlock.gif
while  (iter.hasNext())  dot.gif {
InBlock.gif    File file 
= iter.next();
InBlock.gif    System.out.println(file.getPath());
ExpandedBlockEnd.gif}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值