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

Program plan:

boolean accept(File directory,String filename)

Program:

Definition of DirecotorySearcher class:

import java.util.*;

import java.io.*;

public class DirectorySearcher implements FilenameFilter

{

private String extension;

private ArrayList foundFiles;

public DirectorSearcher(String ext,ArrayList results)

{

this.extension="."+ext;

foundFiles=results;

}

public boolean accept(File dir,String name) //implementation of accept method

{

return name.endsWith(extension); //return true if the file has the given extension.

}

public void matchingFiles(File f)

{

if(f.isFile()) //checking for files

{

if(accept(f,f.getName()) //accept method returns true if the filename is with the given extension

{

foundFiles[]=f.list();return; //then the required file will be added to the ArrayList object

}

else

for(String f:f.listFiles()) //if it is a directory then the process continues to the rest of the sub directories

{fildmatchingFiles(f);}

}

}

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