import java.io.*;
import java.util.*;
public class javaFileList
{
public static void main(String[] args)
{
ArrayList<File> al = new ArrayList<File>();
File f = new File("d:\\java0217");
File f1 = new File("d:\\yuyu.txt");
showDir(f,al);
writeTo(f1,al);
}
public static void writeTo(File f1,ArrayList<File> al)
{
BufferedWriter bw = null;
try
{
bw = new BufferedWriter(new FileWriter(f1));
for(File f :al)
{
String path =f.getAbsolutePath();
bw.write(path);
bw.newLine();
bw.flush();
}
}catch(IOException e)
{
throw new RuntimeException();
}
finally
{
try
{
if(bw!=null)
bw.close();
}catch(IOException e)
{
throw new RuntimeException();
}
}
}
public static void showDir(File f,ArrayList<File> al)
{
File[] files = f.listFiles();
for(int x=0;x<files.length;x++)
{
if(files[x].isDirectory())
{
showDir(files[x],al);
}else
{
if(files[x].getName().endsWith("java"));
al.add(files[x]);
}
}
}
}
------------------------------------------------------------
这里没有写if(!file.exists())
file.createNewFile();
?????????????? |