package com.io.file; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * 把文件列表存储在文本文档中 * @author wjw */ public class FileList_txt { public static void main(String[] args) throws IOException { File file=new File("C:\\Documents and Settings\\zjw\\Workspaces\\MyEclipse 8.6\\LianXi\\src\\com"); List<File> list=new ArrayList<File>(); getList(file,list); BufferedWriter bw=new BufferedWriter(new FileWriter(new File("g:/java/listFiles.txt"))); for(File f:list){ bw.write(f.getAbsolutePath()); bw.newLine(); bw.flush(); } bw.close(); } public static void getList(File file,List<File> list){ File[] f=file.listFiles(); for(File ff:f){ if(ff.isDirectory()){ getList(ff,list);//注意这里调用递归时,传参数要注意 }else{ if(ff.getName().endsWith(".java")){ list.add(ff); } } } } }