TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
我们在开发中,经常需要遍历一个目录下的所有文件,常用的办法就是使用一个函数递归遍历是常用的办法。例如:
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
==
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的例子:
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());
}
源码下载:http://file.javaxxz.com/2014/10/30/235739968.zip |
|