http://files.cnblogs.com/files/xjyggd/html5music.rar
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class directoryTest {
private static ArrayList<String> filelist = new ArrayList<String>();
private static Map<String, String> fileMap = new HashMap<String, String>();
public static void main(String[] args) throws Exception {
String filePath = "G://music//mp3";
getFiles(filePath, "mp3");
StringBuilder stringBuilder = new StringBuilder();
for(String strFile : filelist)
{
String[] strFileSplit = strFile.split("\\");
String strFileName = strFileSplit[strFileSplit.length-1];
System.out.println(strFileName);
if(stringBuilder.length() > 0)
{
stringBuilder.append(",");
}
stringBuilder.append("{name:'" + strFileName + "',author:'',url:'" + strFile.replace("\","\\") + "'}");
}
System.out.println(stringBuilder.toString());
}
/*
* 通过递归得到某一路径下所有的目录及其文件
*/
static void getFiles(String filePath, String pattern){
File root = new File(filePath);
File[] files = root.listFiles();
for(File file:files){
if(file.isDirectory()){
/*
* 递归调用
*/
getFiles(file.getAbsolutePath(), pattern);
//filelist.add(file.getAbsolutePath());
//System.out.println("显示"+filePath+"下所有子目录及其文件"+file.getAbsolutePath());
}
else
{
// System.out.println("显示"+filePath+"下所有子目录"+file.getAbsolutePath());
if (file.getName().contains(pattern))
{
//fileMap.put(file.getName(), file.getAbsolutePath());
filelist.add(file.getAbsolutePath());
// System.out.println(file.getName() + " " + file.getAbsolutePath());
}
}
}
}
}