原文地址:
Java 文件过滤 FileFilter http://blog.csdn.net/xiangyong2008/article/details/5899740
1.写一个类继承与FileFilter
- package com.dream.musicplayer;
-
- import java.io.File;
- import java.io.FileFilter;
-
- 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文件
-
-
-
-
- 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());
- }
- }
-
-
- }
这样就可以获取某个路径下的所有需要获取的文件类型了。