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

Screen Shot 1:

Screen Shot: 2

Code is:

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

import com.sun.org.apache.xpath.internal.FoundIndex;

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).listFiles());
                ArrayList<String> result = ds.printFiles();
                for(String result1:result)
                   System.out.println(result1);

        }

}


class DirectorySearcher
{
        private String extension;
        private ArrayList<String> foundFiles;
        public DirectorySearcher(String ext, ArrayList<String> results)
        {
                extension = ext;
                foundFiles = results;
        }

       public void findMatchingFiles(File[] f)
        {
           for (File file : f)
            {
                if (file.isDirectory())
                {
                   findMatchingFiles(file.listFiles());
                }
                else if(file.isFile() && file.getName().contains(extension))
                {
                   foundFiles.add(file.getAbsolutePath());
                }
            }
       }
       public ArrayList<String> printFiles()
       {
           return foundFiles;
       }
}

Description:

This code is for add all the file path to a arraylist.

The extension of the file must be same with the name entered at runtime.In screen shot 2 the directory structure has been given.

This code recurring until all the subfolder traversed successfully.

The fuction printFiles has been added for print the arraylist which contains all the file name (fulfilling the creteria in question).

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