62,614
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) throws Exception {
// 首先创建文件过滤类
FileFilter fl = new FileFilter() {
@Override
public boolean accept(File pathname) {
String s = pathname.getName().toLowerCase();
if (s.startsWith("abc")) {
return true;
}
return false;
}
};
File f = new File(".");
// 第二部根据过滤器过滤指定目录下的文件、
getFile(fl, f);
}
/**
* 递归调用查找文件
*
* @param fl
* @param src
* @throws FileNotFoundException
* @throws IOException
*/
private static void getFile(FileFilter fl, File src)
throws FileNotFoundException, IOException {
File[] outFile = src.listFiles();
for (File out : outFile) {
if (out.isDirectory()) {
getFile(fl, out);
} else {
File[] inner = src.listFiles(fl);
for (File file : inner) {
InputStream in = new FileInputStream(file);
byte[] bt = new byte[(int) file.length()];
in.read(bt);
OutputStream outs = new FileOutputStream("D:/"
+ new Date().getTime() + "." + file.getName());
outs.write(bt);
}
}
}
}