*****没事写了这个小程序,可以把一个目录下的所有你想要的歌曲文件找出来,生成m3u列表文件,并且自动播放。。希望对大家有所帮助。*****
---------------------------------------------------------------------------------------------------------------------
帮助类:
ReadAllFiles :递归读取指定目录下所有要求格式的文件。
---------------------------------------------------------------------------------------------------------------------
package com.test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class ReadAllFiles {
private List fileList = new ArrayList();//store the file's information
/**
* use recursion find all files information
*
* Get All file's informatioin,just include file's name and file's full path
* @param flag if flag equal "1" then only display file's name,Otherwise display file's name and path
* @return file's informatioin
*/
public List getAllFileInfo(String path,String flag,String[] musicType) {
try {
File file = new File(path);
File[] tempFile = file.listFiles();
for (int i = 0; i < tempFile.length; i++) {
if (tempFile[i].isDirectory()) {
getAllFileInfo(tempFile[i].getPath(),flag,musicType);
} else {
for(int j=0;j<musicType.length;j++){
if(tempFile[i].getName().endsWith(musicType[j])){
if(flag.equals("1")){
fileList.add(tempFile[i].getName());
}else{
fileList.add(tempFile[i].getPath());
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return fileList;
}
}
---------------------------------------------------------------------------------------------------------------------
实现类:
CreateMusicList :生成列表文件并表自动播放。
---------------------------------------------------------------------------------------------------------------------
package com.test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
public class CreateMusicList {
/**
* @param args
*/
public static void main(String[] args) {
//必须已m3u为扩展名结尾,这样就可以生成播放文件列表
String createMusicPath="d:\\MyMusic111.m3u";
System.out.println(createMusicPath);
createMusicList(createMusicPath);//指定生成的文件的名字和位置
try {
Runtime.getRuntime().exec("cmd /c start "+createMusicPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//run
}
public static void createMusicList(String musicPath){
try {
FileWriter f = new FileWriter(musicPath);
BufferedWriter musicFile = new BufferedWriter(f);
ReadAllFiles files=new ReadAllFiles();
List l=files.getAllFileInfo("D:\\music","0",new String[]{"mp3","wma"});//Get file's full name
for(int i=0;i<l.size();i++){
musicFile.write((String)l.get(i));
musicFile.newLine();//create newline
}
musicFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
---------------------------------------------------------------------------------------------------------------------