FileFinder.java.html

  1  import java.io.File;
  2  
  3  public class FileFinder
  4  {
  5     private File[] children;
  6  
  7     /**
  8        Constructs a file finder for a given directory tree.
  9        @param startingDirectory the starting directory of the tree
 10     */
 11     public FileFinder(File startingDirectory)
 12     {
 13        children = startingDirectory.listFiles();
 14     }
 15  
 16     /**
 17        Prints all files whose names end in a given extension.
 18        @param extension a file extension (such as ".java")
 19     */
 20     public void find(String extension)
 21     {      
 22        for (File child : children)
 23        {
 24           String fileName = child.toString();
 25           if (child.isDirectory())
 26           {
 27              FileFinder finder = new FileFinder(child);
 28              finder.find(extension);
 29           }
 30           else if (fileName.endsWith(extension))
 31           {
 32              System.out.println(fileName);
 33           }
 34        }
 35     }
 36  }