【Java】本地目录遍历搜索文件

package modifyWordText;

import java.io.File;
import java.io.FilenameFilter;


/**
* 第一步:遍历目录查找文件
*/

public class BulkSearch {

private static String targetFormat = ".docx";

private static String regex = "(.*?)" + targetFormat;

public String[] test(String source, String target) {
//需要批量操作的目录
File file = new File(source);


//正则逻辑
File[] result = file.listFiles(getFileRegexFilter(regex));


//文件名称列表
String text[] = new String[result.length];


//遍历文件
DocToXml docToXml = new DocToXml();

for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);

String[] field = result[i].toString().split("\\\\");
text[i] = field[field.length - 1].split("\\.")[0];
docToXml.test(result[i].toString(), target, text[i]);
}

return text;

}

//正则匹配
public static FilenameFilter getFileRegexFilter(String regex) {
final String regex_ = regex;

return new FilenameFilter() {
public boolean accept(File file, String name) {
boolean ret = name.matches(regex_);

return ret;
}
};

}

}