import java.io.*;
import java.util.*;
class FileList
{
public static void main(String[] args)
{
File f1=new File("D:\\");
File f2=new File(f1,"Java目录\\2013.4.29.txt");
List<File> l=new ArrayList<File>();
fileToList(f1,l);
writeToFile(l,f2);
}
public static void fileToList(File dirs,List<File> list)
{
File[] fs=dirs.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name)
{
return name.endsWith(".java");
}
});
for(File f : fs)
{
if(f.isDirectory())
fileToList(f,list);
else
list.add(f);
}
}
public static void writeToFile(List<File> list,File file)
{
BufferedWriter bw=null;
try
{
bw=new BufferedWriter(new FileWriter(file));
for(File f : list)
{
String abs=f.getAbsolutePath();
bw.write(abs);
bw.newLine();
bw.flush();
}
}
catch (IOException e)
{
throw new RuntimeException("非法操作");
}
finally
{
try
{
if(bw!=null)
bw.close();
}
catch (IOException e)
{
throw new RuntimeException("非法操作");
}
}
}
}
---------- 运行 ----------
Exception in thread "main" java.lang.RuntimeException: 非法操作
at FileList.writeToFile(FileList.java:47)
at FileList.main(FileList.java:11)
输出完成 (耗时 0 秒) - 正常终止
请各位班竹不吝赐教,谢谢
|
|