转自:http://hi.baidu.com/j2ee_cn/blog/item/1b5e84098468c8216b60fb37.html
package
filter;
import
java.io.File;
import
java.io.FilenameFilter;
public
class
ImageFile
implements
FilenameFilter {
private
String
fileName
;
private
String[]
imageFiles
={
".gif"
,
".jpg"
,
".bmp"
,
".png"
};
private
boolean
isImage()
{
boolean
flag=
false
;
for
(String file:
imageFiles
)
{
if
(
fileName
.endsWith(file))
{
flag=
true
;
break
;
}
}
return
flag;
}
@Override
public
boolean
accept(File dir, String fileName) {
this
.
fileName
=fileName.toLowerCase();
return
isImage();
}
}
package
filter;
import
java.io.*;
public
class
FileListDemo
{
public
static
void
main(String[] args) {
File dir=
new
File(
"D:/
小图
"
);
String[] imageFiles=dir.list(
new
ImageFile());
for
(String file:imageFiles){
System.
out
.println(file);
}
}