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: 3815498 • 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

we can write the code code of the following as below

import java.io.File;
import java.util.ArrayList;

/**
* Find the files in the given directory and subdirectories that end
* with the specified extension.
* @author your name
*
*/
public class DirectorySearcher {
private String extension;
private ArrayList 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 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)
{

//start of code
           String myExtension = ".java";
           //check the condition for file searching
           if (f.isFile() == true) {
               int i = fileName.lastIndexOf('.');
               String ext = fileName.substring(i+1);
               if (ext.equals(myExtension)) {
                   foundFiles.add(f.getPath());
               }
               return;
           }
           else
           {
               for (File sub : f.listFiles()) {
                   findMatchingFiles(sub);
               }
           }
}
}

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