Hello, I need help with my JAVA homework! Thanks This assignment asks you to cre
ID: 3841714 • Letter: H
Question
Hello, I need help with my JAVA homework! Thanks
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.)
Sample runs:
By extension and date:
By name only:
Extra Credit (+10)
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.)
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.