Another version of FileLister

It might be more useful to have a method that returns an ArrayList of the file names instead of just printing them out. The simplest way to do this is to use a recursive helper method that has an extra parameter, namely, the list to which you want to add the new names that are found. You start off by passing in an empty list. You can find the complete code here .
  /**
   * Returns a list of files beneath the given file or directory.
   * @param file
   * @return
   */
  public static ArrayList<String> findFiles(File file)
  {
    // create an empty array list...
    ArrayList arr = new ArrayList<String>();
    
    // pass it into the recursive method
    findFiles(file, arr);
    
    // and return the filled-up ArrayList
    return arr;
  }
  
  /**
   * Recursive helper method includes an array list as an argument.
   * Filenames are added to the array list as they are found.
   * @param file
   * @param list
   */
  private static void findFiles(File file, ArrayList<String> list)
  {
    if (!file.isDirectory())
    {
       list.add(file.getName());
    }
    else
    {
      // recursively search the subdirectory
      for (File f : file.listFiles())
      {
        findFiles(f, list);
      }
    }
  }