如果你知道它在什么目录,那么可以做到,你可以使用FileFilter :final String prefix ="pass_231_"
File dir = ... ;
File[] candidates = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().startsWith(prefix);
}
});
// candidates will contain all files starting with"pass_231_"
如果你需要递归搜索(包括子文件夹),你可以执行以下操作:String prefix ="pass_231_";
File dir = ... ;
List candidates = new ArrayList();
searchRecursively(dir, prefix, candidates);
// candidates will contain all files starting with"pass_231_"
然后searchRecursively看起来像这样:public static void searchRecursively(File dir, final String prefix, List candidates) {
File[] matches = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().startsWith(prefix);
}
});
candidates.addAll(Arrays.asList(matches));
File[] subdirs = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (File subdir : subdirs) {
searchRecursively(subdir, prefix, candidates);
}
}