java_IO_File_综合运用:建立一个指定扩展名的文件的列表

 1 package properties;
 2 
 3 import java.io.BufferedWriter;
 4 import java.io.File;
 5 import java.io.FileWriter;
 6 import java.io.FilenameFilter;
 7 import java.io.IOException;
 8 import java.util.ArrayList;
 9 import java.util.List;
10 
11 import Filter.SuffixFilter;
12 
13 public class File2Text {
14 
15 /**
16  * 获取指定目录下,指定扩展名的文件(包含子目录中的)
17  * 这些文件的绝对路径写入到一个文本文件中。
18  * 
19  * 简单说,就是建立一个指定扩展名的文件的列表。 
20  * 
21  * 思路:
22  * 1,必须进行深度遍历。
23  * 2,要在遍历的过程中进行过滤。将符合条件的内容都存储到容器中。
24  * 3,对容器中的内容进行遍历并将绝对路径写入到文件中。
25  * @param args
26  * @throws IOException 
27  */
28     
29     public static void main(String[] args) throws IOException {
30         File dir = new File("E:\\javaproject\\Studying");
31         List<File> list = new ArrayList<File>();
32         
33         SuffixFilter SuffixFilter = new SuffixFilter(".txt");
34         
35         getFiles(dir,SuffixFilter,list);
36         
37         File destFile = new File(dir,"javalist.txt");        
38         write2File(list,destFile);
39         
40     }
41 
42     public static void getFiles(File dir,FilenameFilter filter,List<File> list) {
43         File[] files = dir.listFiles();
44         for(File file:files){
45             if(file.isDirectory())
46                 getFiles(file,filter,list);
47             else{
48                 //对遍历到的文件进行过滤器的过滤。将符合条件File对象,存储到List集合中。 
49                 if(filter.accept(dir,file.getName())){
50                     list.add(file);
51                 }
52             }
53                 
54         }
55         
56     }
57         public static void write2File(List<File> list,File destFile)throws IOException{
58             
59             BufferedWriter bufw = null;
60             try {
61                 bufw = new BufferedWriter(new FileWriter(destFile));
62                 for(File file : list){
63                     bufw.write(file.getAbsolutePath());
64                     bufw.newLine();
65                     bufw.flush();
66                 }
67                 
68                 
69             } /*catch(IOException e){
70                 
71                 throw new RuntimeException("写入失败");
72             }*/finally{
73                 if(bufw!=null)
74                     try {
75                         bufw.close();
76                     } catch (IOException e) {
77                         
78                         throw new RuntimeException("关闭失败");
79                     }
80             }
81         }
82         
83     }
 1 package Filter;
 2 
 3 import java.io.File;
 4 import java.io.FilenameFilter;
 5 
 6 public class SuffixFilter implements FilenameFilter {
 7 
 8     private String suffix;
 9     //重写构造方法,被调用时可直接传入过滤的文件类型。
10     
11     public SuffixFilter(String suffix) {
12         super();
13         this.suffix = suffix;
14     }
15 
16     public boolean accept(File dir, String name) {
17         return name.endsWith(suffix);   //返回suffix 类型文件
18         //return name.contains(suffix); //返回包含文件名中包含suffix
19     }
20 
21 }

 

posted @ 2013-03-30 23:11  wkai212  阅读(278)  评论(0编辑  收藏  举报