惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
Tenable Blog
Project Zero
Project Zero
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
S
Securelist
Know Your Adversary
Know Your Adversary
Vercel News
Vercel News
IT之家
IT之家
V
V2EX
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
K
Kaspersky official blog
博客园_首页
T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Spread Privacy
Spread Privacy
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
有赞技术团队
有赞技术团队
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
Hugging Face - Blog
Hugging Face - Blog
博客园 - 三生石上(FineUI控件)
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
CXSECURITY Database RSS Feed - CXSecurity.com
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
S
SegmentFault 最新的问题
AWS News Blog
AWS News Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost

博客园 - 温少

Java中的System.nano()很慢 新写了一个Java并发程序设计教程 佛教典故 绝世名将 Google云计算体验感受 我在Google AppEngine上部署了一个Java应用(OpenID测试) 杂谈单点登陆以及相关技术 喜闻我的文章进入“多核技术博客征文” top 30 重读罗素《西方哲学史》关于浪漫主义部分的介绍 随想 javaeye站点被ARP攻击有感 对如何建设一个可运维高可靠的SAAS平台,我终于有了想法!!! 获奖2008年金蝶集团优秀员工感言 也说一种普遍错误使用的LOG方式 2008年总结 关于技术架构师的一些看法 使用JSON替代XML 回想过去几年软件业的荒唐事 JPA这个烂东西
FileIterator
温少 · 2008-06-05 · via 博客园 - 温少

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

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,使得遍历一个目录下的文件如何遍历一个集合中的元素一般操作。

废话少说,代码如下:

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 == nullthrow new NoSuchElementException();

        nextInternal();

return rtValue;
    }
private void nextInternal() {
        current 
= null;if (this.state == nullreturn;for (;;) {
            
if (state.index >= state.files.length) {
                state 
= state.parent;
                
if (state == nullreturn;

                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的例子:

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());
}