JAVA学习之路

常用链接

统计

最新评论

使用FilenameFilter

转自: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);
      }
}

posted on 2008-05-23 17:23 joaquin25 阅读(689) 评论(0)  编辑  收藏 所属分类: Java语言


只有注册用户登录后才能发表评论。


网站导航: