FileFinder2.java.html

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