[转]Java 文件过滤 FileFilter
1.写一个类实现FileFilter接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class MP3FileFilter implements FileFilter { @Override public boolean accept(File file) { if(file.isDirectory()) return true; else { String name = file.getName(); if(name.endsWith(".mp3") || name.endsWith(".mp4")) return true; else return false; } } } |
2.传一个路径,获取改路径下的所有mp3 and mp4文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/** * get all the music file in the rootpath. * @param rootPath */ public void getAllFilePath(String rootPath) { File file = new File(rootPath); File[] files = file.listFiles(new MP3FileFilter()); for(int i=0;i<files.length;i++) { if(files[i].isDirectory()) { getAllFilePath(files[i].getPath()); } else { mArrayListMusicPaths.add(files[i].getPath()); mArrayListMusicNames.add(files[i].getName()); System.out.println(files[i].getPath()); } } } |
原文链接:http://blog.csdn.net/xiangyong2008/article/details/5899740
发表评论