import java.io.*;
import java.util.*;
class FileDemo
{
public static void main(String[] args)
{
File dir=new File("D:");
//showDir(dir,0);
//sop(getSum(10));
//delFile(dir);
List list=new ArrayList();
fileToList(dir,list);
File file=new File(dir,"java文件列表.txt");
writeToFile(list,file.toString());
}
public static void writeToFile(List<File> list,String javaListFile){//创建文件写入输出流,将集合内文件的据对路径储到指定文件
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new FileWriter(javaListFile));
for(File file:list){
String path=file.getAbsolutePath();
bw.write(path);
bw.newLine();
bw.flush();
}
}
catch(IOException e){
throw new RuntimeException("写入文件失败");
}
finally{
try{
if(bw!=null)
bw.close();
}
catch(IOException ex){
throw new RuntimeException("流文件不存在");
}
}
}
public static void fileToList(File dir,List<File> list){//遍历目录,将指定文件存入到参数集合中
File[] files=dir.listFiles();
for(File file:files){
if(file.isDirectory())
fileToList(file,list);
else{
if(file.getName().endsWith(".java"))
list.add(file);
}
}
}
}