Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Searching for files in directories of a filesystem is a common requirement for t

ID: 3815495 • Letter: S

Question

Searching for files in directories of a filesystem is a common requirement for tools like anti-virus programs that search the disk for files containing viruses.
Complete a program called FindFiles that will show all the files under a specified directory that end with the specified file extension. The user will enter the directory to begin the search (the special directory “.” means the current working directory) and the extension of the filename to search for. The program will print all pathnames found underneath that directory that end in the extension.
A sample run of the program to find files under the current working directory that end with the “.java” extension (user input is underlined):
Enter name of directory: .
Enter filename extension to search for: .java
Found these files under directory .:
./src/DirectorySearcher.java
./src/FindFiles.java
Please use the supplied FindFiles main program and the DirectorySearcher class to recursively look in directories for filenames that end in the desired extension.
The DirectorySearcher class’s constructor takes the extension entered by the user and the ArrayList of Strings for found files.
Then the DirectorySearcher’s findMatchingFiles() method takes the starting point for the search, and handles the two aspects of recursion using this pseudocode:
If f.isFile() is true, then
    If f.getPath() ends with the extension, then
        Add f.getPath() to the foundFiles array list
   Return // this is the end of recursion
Else // This must be a directory
   For each subFile in f.listFiles() // This gets all the files in the directory
        Call findMatchingFiles(subFile) // This is the recursive call

Note that an easy way to test the program is to search “.” (current directory) for “.java” files, which should find the DirectorySearcher.java and FindFiles.java files (in addition to any other .java files you might have in the current Eclipse project).

DirectorySearcher.java:

FindFiles.java:

Explanation / Answer

    /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.april;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.filechooser.FileFilter;

/**
* Find the files in the given directory and subdirectories that end with the
* specified extension.
*
* @author your name
*
*/
class DirectorySearcher {

    private String extension;
    private ArrayList<String> foundFiles;

    /**
     * Build a DirectorySearcher object to find matches with the given
     * extension.
     *
     * @param ext Filename extension to match, such as ".java"
     * @param results ArrayList to hold found files
     */
    public DirectorySearcher(String ext, ArrayList<String> results) {
        extension = ext;
        foundFiles = results;
    }

    /**
     * Recursively add files with matching extension to the list of files. If
     * the file is a directory, recursively call the method to add files from
     * the subdirectory to the list.
     *
     * @param f File to check: if a file, check its name; if a directory, check
     * its files
     */
    public void findMatchingFiles(File f) {
        File[] allFiles = f.listFiles(); //get list of all files and folder in the dir
        for (File file : allFiles) {
            if (file.isDirectory()) { //if it is a dir, go recursive :P
                findMatchingFiles(file); //recurrsion
            } else if (file.getName().endsWith("." + extension)) { //match extension the crude way
                foundFiles.add(file.getPath());//if matched, BINGO! add file to the list along with path name
            }
        }
    }

}

public class FindFiles {

    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Enter name of directory to start search: ");
        String directory = console.nextLine();
        System.out.print("Enter filename extension to search for: ");
        String extension = console.nextLine();

        ArrayList<String> foundFiles = new ArrayList<String>();
        DirectorySearcher ds = new DirectorySearcher(extension, foundFiles);
        ds.findMatchingFiles(new File(directory));

        System.out.println("Found these files under directory " + directory + ":");
        for (String f : foundFiles) {
            System.out.println(f);
        }
    }

}

I have edited the code and commented all new lines that were added. I kept it simple and crude. I hope you like it. In case you are facing any trouble, please feel free to comment below. I shall be glad to guide you to success.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote