Loading, please wait...

Saturday, March 14, 2009

FileNameFilter接口应用: 指定多个扩展名过滤规则

java的经常会写一些FileNamesFilter以方面自已获取文件列表.
G.G我捉摩了下这个接口.也写了一个自已的应用来获取能匹配扩展名列表的文件集合.
大家pp.用匿名实现.
/**
 * Returns a list of files that the extension name matchs one of extensions.
 * 

Usage: getFilesByNameExtensions(dir, "txt", "JPEG");
 * @author GreatGhoul http://greatghoul.blogspot.com  * @param dir The directory to scan in.  * @param extensions Extensions collection.  * @return A list of matched files.  */ public static List getFilesByNameExtensions(File dir, final String... extensions) {     return Arrays.asList(dir.listFiles(new FilenameFilter()  {         @Override         public boolean accept(File dir, String name) {             File file = new File(dir, name);             boolean accept = false;             for (String extension : extensions) {                 if (file.isDirectory()) break;                 if (name.toLowerCase().endsWith("." + extension.toLowerCase())) {                     accept = true;                     break;                 }             }             return accept;         }        })); }
Enjory it.

转载声明: 出自: Ghoul To World!作者: GreatGhoul

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.