Javaで指定のフォルダの指定のファイルのリストを返す。


import java.util.*;
import java.io.File;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

protected String[] getFileList(String path, String format) {
		//System.out.println(format + "file's name.");
		ArrayList<String> fileList = new ArrayList<String>();
		// make list
		File dir = new File(path);
		String[] files = dir.list();
		Pattern pattern = Pattern.compile("(\\S+."+ format +")");
		Pattern pattern2 = Pattern.compile("."+ format +"");
		for(int i=0; i<files.length; i++) {
			String[] str = pattern2.split(files[i]);
			Matcher matcher = pattern.matcher(files[i]);
			if(matcher.matches()) { // if format is .wav
				System.out.println(files[i] +" :::: "+ str[0]);
				fileList.add(str[0]);
			} else {
				System.out.println("no match :"+files[i]);
			}
		}
		String[] list = new String[fileList.size()];
		fileList.toArray(list);
		return list;
	}