001package edu.pdx.cs410J.core;
002
003import java.io.*;
004
005/**
006 * This program demonstrates <code>File</code>,
007 * <code>FileFilter</code>, and <code>FilenameFilter</code> by
008 * searching recursively through directories and printing out the
009 * names of Java source files.
010 */
011public class FindJavaFiles {
012
013  private static FileFilter      dirFilter;
014  private static FilenameFilter  javaFilter;
015
016  /**
017   * Prints out the names all of the Java source files in a directory
018   * and then recurses over subdirectories.
019   */
020  private static void findJavaFiles(File dir) {
021    File[] javaFiles = dir.listFiles(javaFilter);
022    for (int i = 0; i < javaFiles.length; i++) {
023      System.out.println(javaFiles[i].toString());
024    }
025
026    File[] dirs = dir.listFiles(dirFilter);
027    for (int i = 0; i < dirs.length; i++) {
028      findJavaFiles(dirs[i]);
029    }
030  }
031
032  /**
033   * The one command line parameter is the directory in which to start
034   * the search.
035   */
036  public static void main(String[] args) {
037    File file = new File(args[0]);
038    if (file.isDirectory()) {
039      dirFilter = new DirectoryFilter();
040      javaFilter = new JavaFilenameFilter();
041
042      // Look for Java files
043      findJavaFiles(file);
044
045    } else {
046      System.err.println(file + " is not a directory");
047    }
048  }
049
050}