方法一:
http://blog.csdn.net/zjx102938/article/details/8114114
import java.io.File;
import java.util.ArrayList;
import java.util.List; public class FileFind {
public static void main(String[] args) { String path = "D:\\Test";
List data=new ArrayList();
data=getData(path, data);
for (int i=0;i<data.size();i++) {
System.out.println(data.get(i));
}
} private static List getData(String path, List data) { File f=new File(path);
if (f.isDirectory()) {
File[] fs=f.listFiles();
for (int i=0;i<fs.length;i++) {
data=getData(fs[i].getPath(), data);
}
} else if (f.getName().endsWith(".java")) {
data.add(f.getName());
}
return data;
}
}
方法二:
package test; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList; public class SearchSuffixIndir { private static String path = "E:\\myeclipse\\mirror_parse\\WebRoot\\mirror"; /**
*
* 将给定目录下的及子目录下的 所有的给定后缀名的文件路径打印到一个文件中。
*
* 深度遍历该父目录,将符合条件的文件路径存到集合中。(进行过滤)。
*
* 再将集合写到文件中。
*
*
*/
public static void main(String[] args) { File dir = new File(path); File file = new File(dir,"suffix.txt");
//内部类
FilenameFilter searchSuffix = new FilenameFilter(){
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".html");
}
}; ArrayList<File> path = new ArrayList<File>(); writeToArr(dir,searchSuffix,path);
writeTofile(path,file);
} private static void writeTofile(ArrayList<File> al, File file) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file)); for(File f: al){
new String();
// bw.write(String.valueOf(f.length()));
bw.write(f.getAbsolutePath());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
throw new RuntimeException("写入失败!");
}finally{
if(bw != null)
try {
bw.close();
} catch (IOException e) {
throw new RuntimeException("关闭失败!");
}
} } public static void writeToArr(File dir, FilenameFilter searchSuffix, ArrayList<File> al) { File []files = dir.listFiles();
for(File f : files){
if(f.isDirectory()){
//递归了。
writeToArr(f, searchSuffix, al);
}else{
if(searchSuffix.accept(dir, f.getName())){
System.out.println(f);
al.add(f);
}
}
}
} }