本帖最后由 黑马张伟 于 2011-12-24 08:09 编辑
package file;
import java.io.*;
import java.util.*;
public class JavaFileList {
public static void main(String[] args) throws IOException{
File dir = new File("E:\\myjob");
List<File> list = new ArrayList<File>();
fileToList(dir,list);
//System.out.println(list.size());
//String name="";
File file=new File(dir,"java.txt");//把所有
WriteToFile(list,file.toString());
}
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(".html"));
list.add(file);
}
}
}
public static void WriteToFile(List<File> list,String javaListFile) throws IOException
{
BufferedWriter bufw = null;
try{
bufw = new BufferedWriter(new FileWriter(javaListFile));
for(File f:list)
{
String path = f.getAbsolutePath();
bufw.write(path);
bufw.newLine();
bufw.flush();
}
}
catch(IOException e)
{
throw e;
}
finally
{
try {
if(bufw!=null)
bufw.close();
} catch (IOException e2) {
throw e2;
}
}
}
} |