- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class JavaFileList {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- File dir = new File("D:\\java");
- List<File> list = new ArrayList<File>();
- file2List(dir,list);
-
- File f = new File(dir,"javaList.txt");
- write2File(list,f.toString());
- }
-
- public static void file2List(File dir,List<File> list){
- File[] arr = dir.listFiles();
- for(File f: arr){
- if(f.isDirectory()){
- file2List(f,list);
- }
- else {
- if(f.getName().endsWith(".java"))
- list.add(f);
- }
- }
- }
-
- public static void write2File(List<File> list,String javaListFile)throws IOException{
- BufferedWriter bw = null;
- try{
- bw = new BufferedWriter(new FileWriter(javaListFile));
- for(File f:list){
- String path = f.getAbsolutePath();
- bw.write(path);
- bw.newLine();
- bw.flush();
- }
- }catch(IOException e){
- throw e;
- }
- finally{
- try{if(bw!=null)
- bw.close();
- }catch(IOException e){
- throw e;
- }
-
- }
- }
- }
复制代码 以前写的,没加注释,有兴趣你加一些 |