Create a program searches all files


Discuss the below code:

Q: Write a program Find that searches all files specified on the command line and prints out all lines containing a reserved word. For example, if you call java Find ring report.txt address.txt Homework.java, then the program might print:

report.txt: has broken up an international ring of DVD bootleggers that
address.txt: Kris Kringle, North Pole
address.txt: Homer Simpson, Springfield
Homework.java: String filename;

The reserved word is always the first command line argument.

Here is a sample program run:

java Find lamb mary1.txt mary2.txt

mary1.txt: Mary had a little lamb,
mary1.txt: little lamb, little lamb,
mary1.txt: Mary had a little lamb, its fleece was white as snow.
mary1.txt: and everywhere that Mary went, the lamb was sure to go.
mary2.txt: it made the children laugh and play to see a lamb at school.

Use the following class as your main class:

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

/**
This program searches files and prints out all lines containing
a keyword.
*/
public class Find
{
public static void main(String[] args) throws FileNotFoundException
{
if (args.length < 2)
{
System.out.println("Usage: Find keyword sourcefile1 sourcefile2 . . .");
return;
}
String keyword = args[...];
for (int i = 1; i < ...; i++)
{
String filename = args[...];
. . .
}
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a program searches all files
Reference No:- TGS01934429

Now Priced at $25 (50% Discount)

Recommended (97%)

Rated (4.9/5)