Question

In: Computer Science

In this assignment, you will create a Java program to search recursively for a file in...

In this assignment, you will create a Java program to search recursively for a file in a directory.

• The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name.

• If incorrect number of parameters are given, your program should print an error message and show the correct format.

• Your program must search recursively in the given directory for the files whose name contains the given filename. Once a match is found, your program should print the full path of the file, followed by a newline.

• You can implement everything in the main class. You need define a recursive method such as: public static search(File sourceFolder, String filename) For each folder in sourceFolder, you recursively call the method to search.

• A sample run of the program may look like this: //The following command line example searches any file with “Assignment” in its name

%java Assign7.Assignment6

C:\CIS 265 Assignment

C:\CIS 265\AS 2\Assignment2.class C:\CIS 265\AS 2\Assignment2.java C:\CIS 265\AS 3\CIS265\Assignment3.class C:\CIS 265\AS 3\CIS265\Assignment3.java C:\CIS 265\AS 4\Assignment4.gpj

Hi please tell me what to input exactly, thanks

Solutions

Expert Solution

You only need to input the source folder where you need to search for the filename and the filename which you need to search. You need to take arguments from command line. i will write the main function for you

public class Demo{
    public static search(File sourceFolder, String filename) {
        if(sourceFolder.isDirectory()){
            File[] arr=sourceFolder.listFiles();//list of all files in dir
            for(int i=0;i<arr.length;i++){
                search(arr[i],filename);//recursively searches in child dirs
            }
        }
        else if(sourceFolder.isFile()){
            if((sourceFolder.getName()).contains(filename)){//checks if filename is substring
                System.out.println(sourceFolder.getPath());//prints path of file
            }
            return;
        }
    }
     public static void main(String []args){
        if(args.length!=2){
            System.out.println("Please enter 2 arguments, 1st containing the name of the folder and the 2nd containg the filename you want to search for");
            System.exit(0);
        }
        String folder=args[0]; // gets path of dir given
        String filename=args[1];// gets filename from user.
        File sourceFolder = new File(folder);
        search(sourceFOlder,filename);
     }
}

Related Solutions

I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale (POS) system. The solution must be in JAVA language. You create the main program called POSmain.java and two classes: ShoppingCart and CashRegister. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer to do the following: 1) Create a bank account by supplying a user id and password .2) Login using their id and password 3) Quit the program. Now if login was successful the user will be able to do the following: 1) Withdraw money. 2) Deposit money. 3) Request balance. 4) Quit the program. If login was not successful (for example the id or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT