Searching the files in directories of a filesystem is a common requirement for t
ID: 3939927 • Letter: S
Question
Searching the 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).
FindFiles.java:
Explanation / Answer
Please follow the code and comments for description :
CODE :
a) DirectorySearcher.java :
import java.io.File; // required imports for the code to run
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 { // class to run the code
private String extension; // instance variables
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) { // constructor with the required parameters
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) { // method that checks for the matching pattern recursively
// Use the pseudocode outlined in the homework
// to help solve this recursive method.
if (f.isFile()) { // check if the name is a file
if (f.getPath().endsWith(extension)) { // if yes then check for the ending extension
foundFiles.add(f.getPath()); // if yes then get the path of the file
}
return; // this is the end of recursion
} else { // This must be a directory
for (File subfiles : f.listFiles()) { // This gets all the files in the directory
findMatchingFiles(subfiles); // This is the recursive call
}
}
}
}
b) FindFiles.java :
import java.io.File; // required imports
import java.util.ArrayList;
import java.util.Scanner;
public class FindFiles { // class to run the code
public static void main(String[] args) { // driver method
Scanner console = new Scanner(System.in); // scanner call to get the data from the user
System.out.print("Enter the Name of the Directory to Start the Search : "); // prompt to enter the directory
String directory = console.nextLine(); // get the data
System.out.print("Enter the Filename Extension to Search for : "); // prompt to enter the extension
String extension = console.nextLine();// get the data
ArrayList foundFiles = new ArrayList(); // initialise a new array list
DirectorySearcher ds = new DirectorySearcher(extension, foundFiles); // create a object to the class by passing the arguments
ds.findMatchingFiles(new File(directory)); // call the method to get the lsit of matches
System.out.println("Found these files under directory '" + directory + "' : "); // print the data to console
for (Object f : foundFiles) {
System.out.println(f);
}
}
}
OUTPUT :
Enter name of directory to start search: .
Enter filename extension to search for: .java
Found these files under directory '.' :
.srcjavaFilesAccount.java
.srcjavaFilesAdditionGame.java
.srcjavaFilesApproveVotingBallots.java
.srcjavaFilesSortInput.java
.srcjavaFilesStudentGrades.java
.srcjavaFilessubset.java
.srcjavaFilesSubtractionGame.java
.srcjavaFilesUserChoice.java
.srcjavaFiles_newreaOfCircle.java
.srcjavaFiles_newArray2D.java
.srcjavaFiles_newArraylist.java
.srcjavaFiles_newAss140.java
.srcjavaFiles_newBankAccount.java
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.