import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class JavaFilelist {
public static void main(String[] args) throws IOException{
File dir=new File("D:\\");
List<File> list=new ArrayList<File>();
fileToList(dir,list);
File file=new File("dir","list.txt");
writeToFile(list,file.toString());
System.out.println(list.size());
}
private static void writeToFile(List<File> list, String string) {
// TODO Auto-generated method stub
}
public static void writeToFile(List<File> list,File dir)throws IOException{
BufferedWriter bufw=null;
try
{
bufw=new BufferedWriter(new FileWriter("D:\\java.txt"));
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 e)
{
throw e;
}
}
}
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(".txt"))
list.add(file);
}
}
}
}
|
|