FindFile Class Interface FindFile(int maxFiles): This constructor accepts the maximum number of files to find. void directorySearch(String target, String dirName): The parameters are the target file name to look for and the directory to start in. This will save the file locations (i.e., file path, starting from dirName) in some private class data structure. int getCount(): This accessor returns the number of matching files found String[] getFiles(): This getter returns the array of file locations, up to maxFiles in size.

Respuesta :

Answer:

Java program for the problem is explained below

Explanation:

FindFile.java

import java.io.File;

import java.io.FilenameFilter;

/**

*

* @author dannwaeze

*/

public class FindFile {

String targetFile;

String pathToSearch;

int count;

private String[] filenames;

int maxFiles;

 

public FindFile(int maxFiles) {

this.maxFiles = maxFiles;

this.count=0;

}

//getter setter metthods

public String getTargetFile() {

return targetFile;

}

public void setTargetFile(String targetFile) {

this.targetFile = targetFile;

}

public String getPathToSearch() {

return pathToSearch;

}

public void setPathToSearch(String pathToSearch) {

this.pathToSearch = pathToSearch;

}

public int getCount() {

return count;

}

public void setCount(int count) {

this.count = count;

}

public String[] getFilenames() {

return filenames;

}

public void setFilenames(String[] filenames) {

this.filenames = filenames;

}

 

//file searching method

public void directorySearch(String target, String dirName){

File dir = new File(dirName);

FilenameFilter filter = new FilenameFilter() {

@Override

public boolean accept

(File dir, String name) {//filtering by filename given

return name.startsWith(target);

}

};

this.filenames = dir.list(filter);

this.count = this.filenames.length;

if (this.filenames == null) { //checking for empty direxxtory or invalid directory name

System.out.println("Directory is not found...");

}

else {

for (int i=0; i<this.filenames.length; i++) {

String filename = this.filenames[i];

System.out.println(filename);

}

}

}

}

------------------------------------------------------------------------------------------------------------------------------

Driver.java

public class Driver {

public static void main(String args[]){

//declaring variablaes

String targetFile = "lesson.css";

String pathToSearch ="C:\\WCWC:";

//instacnce created

FindFile finder = new FindFile(10);

//calling function

finder.directorySearch(targetFile, pathToSearch);

}

}