本帖最后由 吴亨 于 2012-1-12 15:47 编辑
import java.io.*;
import java.util.*;
class MyJavaList1
{
public static void main(String[] args) throws Exception
{
File dir = new File("D:\\毕老师课件\\代码");
List<File> list = new ArrayList<File>();
fileToList(dir,list);
System.out.println(list.size());
FileWriter fw = new FileWriter("d:\\index2.txt");
writeToFile(list,fw);
fw.close(); //如果此处不关闭流资源,请问可以将内容完整写进index2.txt里吗?
}
public static void fileToList(File dir,List<File> list) throws Exception
{
File[] files = dir.listFiles();
for(File file : files)
{
if(file.isDirectory())
fileToList(file,list);
else
{
if(file.getPath().endsWith(".java"))
list.add(file);
}
}
}
public static void writeToFile(List<File> list,FileWriter fw) throws Exception
{
int count = 0;
for(File file : list)
{
fw.write(file.getPath()+"................"+(++count));
fw.write('\r');
fw.write('\n');
}
System.out.println(count);
}
}
|