Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I need help for this Java homework: A Recursive Directory Search Utility f

ID: 3843460 • Letter: H

Question

Hello

I need help for this Java homework:

A Recursive Directory Search Utility for Windows

This assignment combines recursion, looping and advanced file handling in a practical application for the Windows operating system’s file structure.

This assignment asks you to create a Java program that brings recursion, looping, file handling techniques and (optionally) advanced file date processing together in a useful Windows application. Your program will prompt the user for search criteria and will then ‘crawl’ through a directory together with its subdirectories looking for files that qualify. Results of the search are displayed.

Your program should search for files:

* By name (without an extension – e.g. ‘MyTextFile’)

* By extension name (e.g. ‘java’)

* By content (e.g. ‘Math.sqrt’)

* By modification date (optional extra credit – see below)
Notes:

* The above criteria for searching may be combined in any form (see example runs below.)

* A valid initial path will always be entered – other criteria are optional.

* You should allow the user to find file name entries regardless of upper/lower
case characters in the file and extension names.

* Searching for content, however, should be case sensitive.

* The count of qualifying files should be displayed (see example output.)

* Your class (and your corresponding .java file) should be named as follows:
o MySearch

o Zip your .java file

Sample runs:

By extension and date:

Directory search by path, name, extension, content and date.

Enter starting directory for the search (like c: emp): c: emp

Enter the file name (like myFile or enter for all):

Enter the file extension (like txt or enter for all): txt

Enter content to search for (like cscd211 or enter for any):

Enter last modified date (like 11/21/2013 or enter for any): 11/1/2011

Results - 3 entries found:

c: empAdobe est.txt

c: empx est.txt

c: empx estx.txt

By name only:

Directory search by path, name, extension, content and date.

Enter starting directory for the search (like c: emp): c:eclipseprojects

Enter the file name (like myFile or enter for all): mysearch

Enter the file extension (like txt or enter for all):

Enter content to search for (like cscd211 or enter for any):

Enter last modified date (like 11/21/2011 or enter for any):

Results - 2 entries found:

c:eclipseprojectsworkspaceRecursionDirectorySearchAssignmentinMySearch.class

c:eclipseprojectsworkspaceRecursionDirectorySearchAssignmentsrcMySearch.java

By extension, content and date:

Directory search by path, name, extension, content and date.

Enter starting directory for the search (like c: emp): c:ewucscd210

Enter the file name (like myFile or enter for all):

Enter the file extension (like txt or enter for all): java

Enter content to search for (like cscd211 or enter for any): Math.sqrt

Enter last modified date (like 11/21/2011 or enter for any): 1/1/2009

Results - 5 entries found:

c:ewucscd210Distance.java

c:ewucscd210LabTriangle.java

c:ewucscd210SieveOfEratosthenes.java

c:ewucscd210Test2.java

c:ewucscd210TestAra.java

In addition to the required search criteria, also provide the ability to search by ‘date last modified’. For this task, you would do well to take advantage of the Calendar class. Note the ‘lastModified’ value for files is retained in milliseconds since 1/1/1970 (the beginning of computer time.)

Thank you.

Explanation / Answer

package javaapplication8;

import java.io.BufferedReader;
import java.io.File;import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

/**
*
* @author Rashmi Tiwari
*/
class GenericExtFilter implements FilenameFilter {

       private String extension;

       public GenericExtFilter(String extension) {
           this.extension = extension;
       }

       public boolean accept(File directory, String name) {
           return (name.endsWith(extension));
       }
}
public class FileSearch {

private static String fileName;

private static String directoryName;
  
private List<String> answer=new ArrayList<String>();

public String getFileName() {
return fileName;
}

public void setFileName(String fileName) {
this.fileName = fileName;
}

public String getDirectoryName() {
return directoryName;
}

public void setDirectoryName(String directoryName) {
this.directoryName = directoryName;
}

public List<String> getAnswer() {
return answer;
}

  
public static void main(String args[]){

BufferedReader bout=new BufferedReader(new InputStreamReader(System.in));
FileSearch fileSearch=new FileSearch();
try{
System.out.println("Enter the file name to search ");
String fName=bout.readLine();
System.out.println("Enter the directory in which you want to search");
String dName=bout.readLine();
System.out.println("Enter the file extension ");
String extension=bout.readLine();
System.out.println("Enter the content thorugh which you want to search");
String content=bout.readLine();
System.out.println("Enter 1 to search by file Name");
System.out.println("Enter 2 to search by file extension");
System.out.println("Enter 3 to search by file Content");
System.out.println("Enter 4 List File according to last modified");
System.out.println("Enter 5 to exit");
System.out.println("Enter your choice");
int ch=bout.read();
switch(ch){
case 1:
fileSearch.searchIntoDirectory(new File(dName),fName);
break;
case 2:
fileSearch.searchIntoDirectoryWithExtension(dName,extension);
break;
case 3:
fileSearch.searchIntoDirectoryWithContent(directoryName, content);
break;
case 4:
fileSearch.searchIntoDirectoryWithLastModified(directoryName);
break;
case 5:
System.exit(0);
default:
System.out.println("Invalid option");
}
int count = fileSearch.getAnswer().size();
   if(count ==0){
   System.out.println(" No result found!");
   }else{
   System.out.println(" Found " + count + " result! ");
   for (String matched : fileSearch.getAnswer()){
       System.out.println("Found : " + matched);
   }
   }
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void searchIntoDirectory(File directoryName,String fileName){
setFileName(fileName);
if(directoryName.isDirectory()){
searchInDirectory(directoryName);
}else{
System.out.println(directoryName.getAbsoluteFile()+"is not directory");
}
}
public void searchInDirectory(File directoryName){
if(directoryName.isDirectory()){
System.out.println("Reading the directory"+directoryName.getAbsolutePath());
if(directoryName.canRead()){
for(File file:directoryName.listFiles()){
if(file.isDirectory()){
searchInDirectory(file);
}else{
if(getFileName().equals(file.getName().toLowerCase())){
answer.add(file.getAbsolutePath().toString());
}
}
}
}else{
System.out.println(directoryName.getAbsoluteFile()+"Access is denied for this directory");
}
}
}
  
public void searchIntoDirectoryWithExtension(String directoryName,String extension){
GenericExtFilter filter = new GenericExtFilter(extension);

       File directory = new File(directoryName);

       if(directory.isDirectory()==false){
           System.out.println("Directory does not exists : " + directoryName);
           return;
       }

       String[] list = directory.list(filter);

       if (list.length == 0) {
           System.out.println("no files end with : " + extension);
           return;
       }

       for (String file : list) {
           String temp = new StringBuffer(directoryName).append(File.separator)
                   .append(file).toString();
          
       answer.add(temp);
}
  
}
public void searchIntoDirectoryWithContent(String directoryName,String content){

File directory = new File(directoryName);

if(directory.isDirectory()){
System.out.println("Reading the directory"+directory.getAbsolutePath());
if(directory.canRead()){
for(File file:directory.listFiles()){
if(file.isDirectory()){
searchIntoDirectoryWithContent(file.getAbsolutePath(),content);
}else{
try{
Scanner scanner = new Scanner(file);
if (scanner.findWithinHorizon(content, 0) != null) {
answer.add(file.getName());
}
scanner.close();}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
}
}else{
System.out.println(directory.getAbsoluteFile()+"Access is denied for this directory");
}
}
  
}
  
public void searchIntoDirectoryWithLastModified(String directoryName){
  
File directory=new File(directoryName);
File [] files = directory.listFiles();

Arrays.sort( files, new Comparator<Object>()
{
public int compare(Object o1, Object o2) {

if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}

});
  
for(File file:files){
answer.add(file.getName());
}
}
}

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