java:文件Filter
2016-01-01 11:40
471 查看
.写一个类继承与FileFilter
[java]
view plaincopyprint?
package com.dream.musicplayer;
import java.io.File;
import java.io.FileFilter;
public class MP3FileFilter implements FileFilter {
@Override
public boolean accept(File file) {
// TODO Auto-generated method stub
// return false;
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文件
[java]
view plaincopyprint?
/**
* 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());
}
}
}
.写一个类继承与FileFilter
[java]
view plaincopyprint?
package com.dream.musicplayer;
import java.io.File;
import java.io.FileFilter;
public class MP3FileFilter implements FileFilter {
@Override
public boolean accept(File file) {
// TODO Auto-generated method stub
// return false;
if(file.isDirectory())
return true;
else
{
String name = file.getName();
if(name.endsWith(".mp3") || name.endsWith(".mp4"))
return true;
else
return false;
}
}
}
package com.dream.musicplayer; import java.io.File; import java.io.FileFilter; public class MP3FileFilter implements FileFilter { @Override public boolean accept(File file) { // TODO Auto-generated method stub // return false; 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文件
[java]
view plaincopyprint?
/**
* 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());
}
}
}
相关文章推荐
- 【Java EE 学习 80 下】【调用WebService服务的四种方式】【WebService中的注解】
- Java读书笔记(1)-异常处理
- Spring Batch入门教程及其框架搭建
- 2016/1/1 运算符 笔记整理 接2015/12/30 Java 语法
- Spring不能注入Static变量的原因及Spring注入静态变量
- Eclipse搭建android环境及Genymotion模拟器安装问题解决方法
- Java在制作jar包时引用第三方jar包的方法
- Java对象的硬、软、弱和虚引用
- android eclipse 环境搭建
- Eclipse启动时出现错误 An internal error occurred during: “Updating indexes”
- 导致MyEclipse内存不足的原因分析及解决办法
- JAVA关于This的用法
- java interface 中定义的class 是否是static 的验证
- java装饰模式
- MyEclipse和Eclipse区别与联系
- Java 48个关键字
- spring-framework-3.2.4与hibernate-release-4.3.5下使用HibernateDaoSupport抛出异常
- Java 模拟队列(一般队列、双端队列、优先级队列)
- Android N 将替代使用 OpenJDK
- [Eclipse] Importerror no module named pwd-----getpass.getuser()