FileIterator

我们在开发中,经常需要遍历一个目录下的所有文件,常用的办法就是使用一个函数递归遍历是常用的办法。例如:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> public   static   void  iterateFile(File file) {
     if  (file.isDirectory()) {
         if  (file.getName().startsWith( " . " ))  return ;

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

//  do something 
}

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

废话少说,代码如下:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> 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的例子:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--> File dir  =   new  File( " /home/wenshao/workspace " );

Iterator < File >  iter  =   new  FileIterator(dir);
while  (iter.hasNext())  {
    File file = iter.next();
    System.out.println(file.getPath());
}

FileIterator相关推荐

  1. 用jQuery实现文件的上传,后台为spring+mybatis

    1.首先为html代码以及js代码 <div id="uploadDiv" style="display:none;"><!-- The fi ...

  2. java学习:Iterator数据加工厂

    前不久听一大神讲课,期间提到Iterator,别看它方法简单,可是组装起来就像"数据加工厂"一样! 下面详细一记 首先略介绍一下Iterator的三个主要方法: next()获得序 ...

  3. tesseract-ocr 识别中文扫描图片

    项目主页地址:http://code.google.com/p/tesseract-ocr/ 相关资源下载地址:http://code.google.com/p/tesseract-ocr/downl ...

  4. word,ppt等office文档转化为pdf进行展示(POI + iText)(亲测有效)

    word,ppt等office软件转化为pdf进行展示(POI +iText)(亲测有效) 1. ppt转化为pdf,利用java的POI和itext进行转化 2.wordtopdf 3.处理pptx ...

  5. springboot集成hadoop实战

    springboot集成hadoop实现hdfs增删改查 maven坐标 <dependency><groupId>org.apache.hadoop</groupId& ...

  6. Java Tesseract-OCR 4.0 windows 测试图像识别

    Java Tesseract-OCR 4.0 windows 测试图像识别:这里需要注意Tesseract-OCR 3.0.1以上才支持中文.我们拿到网上文章的代码来测试不一定对,但是需要找出为什么不 ...

  7. Java OCR 图像智能字符识别技术[可识别中文]

    http://www.open-open.com/lib/view/open1363156299203.html 国内最专业的OCR软件只有2家,清华TH-OCR和汉王OCR,看了很多的OCR技术发现 ...

  8. java suggest_lucene的suggest(搜索提示功能的实现)

    1.首先引入依赖 org.apache.lucene lucene-suggest 7.2.1 2.既然要进行智能联想,那么我们需要为提供联想的数据建立一个联想索引(而不是使用原来的数据索引),既然要 ...

  9. ServletFileUpload 图片上传

    ServletFileUpload 图片上传 <script type="text/javascript"> $(function () { $('#uploadSub ...

最新文章

  1. Linux环境编译安装OpenJDK
  2. Android-02:使用SharedPreferences存储简单数据
  3. led动态显示 c语言,单片机LED点阵的纵向移动(动态显示)
  4. JVM调优:GC 参数
  5. 停止Hadoop或HBase集群的脚本
  6. Windows Eclipse Maven 安装
  7. JAVA对map进行分组
  8. java计算机毕业设计网络教学系统源码+系统+数据库+lw文档
  9. 云服务三兄弟:公有云、私有云和混合云有何不同
  10. linux设置display参数,Linux DISPLAY 变量设置
  11. 三只松鼠商品评论分析
  12. zerotier异地搭建组网
  13. 人工智能与大数据行业的兴起,主要产生了哪些新的职位?
  14. 展锐智能机平台sc9820e调试pwm背光所遇问题小结
  15. ABAQUS 2020 license冲突解决
  16. 基于Java基础-面向对象实现植物大战僵尸简易版
  17. WideDeep论文翻译
  18. Android webm转mp4,Android中直接播放webm的视频an – 手机爱问
  19. centos8 安装中文输入法详细教程
  20. FAT32转NTFS及失败解决方案

热门文章

  1. 计算机技术专业面试英语翻译,面试英文翻译:你为什么选择这个专业
  2. Quad Industries、Agfa等公司合作推出塑料12位RFID标签和带有丝印印刷电路的读出
  3. Oracle基础包之DBMS_STATS(二十)
  4. 《Imperfect C++中文版》——1.2 编译期契约:约束
  5. 计算机毕业设计之android平台的出租打车软件app(源码+系统+mysql数据库+Lw文档)
  6. 计算机二级WPS Office选择题真题第一套,计算机二级wps office考试题库,计算机二级wps office题库
  7. Ubuntu永久修改IP
  8. 【SVN】VS2015和SVN合作
  9. python安装第三方库错误No matching distribution found for cfg
  10. Spring是什么?关于Spring家族