The File class has a deceiving(欺骗;行骗——误导) name;you might think it refers to(引用——指代) a file,but it does't.In fact,"FilePath" would have been a better name for the class.It can represent either the name of a particular file or the names of a set of files in a directory.If it's a set of files,you can ask for that set using the list() method,which returns an array of String.It makes sense to return an array rather one of the flexible container classes,because the number of elements is fixed,and if you want a different derectory listing(不同的目录列表),you just create a different File object.This section shows an example of the use of this class,including the associated FilenameFilter interface.

A directory lister

 
  
  1. import java.io.File; 
  2. import java.io.FilenameFilter; 
  3. import java.util.Arrays; 
  4. import java.util.regex.Pattern; 
  5. public class DirList { 
  6.     public static void main(String[] args){ 
  7.         //File path = new File("."); 
  8.         File path = new File("./src"); 
  9.         String[] list; 
  10.         if(args.length == 0){ 
  11.             list = path.list(); 
  12.         }else
  13.             list = path.list(new DirFilter(args[0])); 
  14.         } 
  15.         Arrays.sort(list,String.CASE_INSENSITIVE_ORDER); 
  16.         for(String dirItem : list){ 
  17.             System.out.println(dirItem); 
  18.         } 
  19.     } 
  20.          
  21.  
  22.  
  23. class DirFilter implements FilenameFilter{ 
  24.     private Pattern pattern; 
  25.      
  26.     public DirFilter(String regex){ 
  27.         pattern = Pattern.compile(regex); 
  28.     } 
  29.      
  30.     @Override 
  31.     public boolean accept(File dir, String name) { 
  32.         return pattern.matcher(name).matches(); 
  33.     } 

The DirFilter class implements the interface FilenameFilter.Notice how simple the FilenameFilter interface is:

 
  
  1. public interface FilenameFilter{ 
  2.     public boolean accept(File dir, String name); 

DirFilter's sole reason for existence is to provide the accept() method to the list() method so that list() can "call back" accept() to determine which file names should be included in the list.Thus,this structure is ofter referred to as(被称为…) a callback.More specifically(更具体地说),this is an example of the Strategy design pattern,because list() implements basic functionality(实现了基本的功能),and you provide the Strategy in the form of a FilenameFilter in order to complete the algorithm necessary for list() to provide its service.(按照FilenameFilter的形式提供了这个策略,以便完善list()在提供服务时所需的算法)Because list() takes a FilenameFilter object as its argument,it means that you can pass an object of any class that implements FilenameFilter to choose(even at run time)how the list() method will behave.()the purpose of a Strategy is to provide flexibility in the behavior of code.(策略的目的就是提供了代码行为的灵活性)

 

The accept() method must accept a File object representing the directory that a particular file is found in,and a String containing the name of that file.Remember that the list() method is calling accept() for each of the file names in the directory object to see which one should be included;this is indicated by the boolean result returned by accept().

 

 

accept() uses a regular expression matcher object to see if the regular expression regex matches the name of the file.(accept会使用一个正则表达式的matcher对象,来查看此正则表达式regex是否匹配这个文件的名字。)Using accept(),the list() method returns an array.

 Anonymous inner classes

This example is ideal(合适的) for rewriting using an anonymous inner class(described in Inner Classes).As a first cut(首先),a method filer() is created that returns a reference to a FilenameFilter:
 
Note that the argument to filter() must be final.This is required by the anonymous inner class so that it can sue an object from outside its scope.(注意,传向filter()的参数必须是final的。这在匿名内部类中是必需的,这样它才能够使用来自该类范围之外的对象)This design is an improvement because the FilenameFilter class is now tightly bound to DirList2.However,you can take this approach one step further and define the anonymous inner as an argument to list(),in which case it's even smaller:(然而,我们可以进一步修改该方法,定义一个作为list()参数的匿名内部类;这样一来程序会变得更小;)
 
  
  1. import java.io.File; 
  2. import java.io.FilenameFilter; 
  3. import java.util.Arrays; 
  4. import java.util.regex.Pattern; 
  5.  
  6. public class DirList3 { 
  7.     public static void main(final String args[]){ 
  8.         File path = new File("."); 
  9.         String[] list; 
  10.         if(args.length == 0){ 
  11.             list = path.list(); 
  12.         }else
  13.             list = path.list(new FilenameFilter(){ 
  14.                 private Pattern pattern = Pattern.compile(args[0]); 
  15.  
  16.                 @Override 
  17.                 public boolean accept(File dir, String name) { 
  18.                     return pattern.matcher(name).matches(); 
  19.                 } 
  20.             }); 
  21.         } 
  22.         Arrays.sort(list,String.CASE_INSENSITIVE_ORDER); 
  23.         for(String dirItem:list){ 
  24.             System.out.println(dirItem); 
  25.         } 
  26.     } 

 111