Very complicated java prorgamming question(Based on Files and Streams)

SoccerBoss
Category:
Programming

Question description


create a copy of the code given above and name it copyfilter. modify the code so that it only writes lines into the output file (from the input file) if they contain a specific word or set of words (i.e. a specific string as part of the line). you may hard code this string to search for at first, but then work to make your program more flexible. hints: identify the part of the program where the line is written into the new file and modify it with a decision structure that only writes it if it contains the text we're looking for.
import java.io.*;

public class copyfilter {

public static void main(string args[]) {
//... get two file names from parameters on command line.
if (args.length != 2) {
system.err.println("usage: java copytextfile sourcefile targetfile");
system.exit(1);
}

//... create file objects.
file infile = new file(args[0]); // read from first file specified
file outfile = new file(args[1]); // write into second file

//... enclose in try..catch because of possible io exceptions.
try {
//... create reader and writer for text files.
bufferedreader reader = new bufferedreader(new filereader(infile));
bufferedwriter writer = new bufferedwriter(new filewriter(outfile));

//... loop as long as there are input lines.
string line = null;
while ((line=reader.readline()) != null) {
writer.write(line);
writer.newline(); // write system-dependent end of line.
}
//... close reader and writer.
reader.close(); // close to unlock.
writer.close(); // close to unlock and flush to disk.

} catch (ioexception e) {
system.err.println(e);
system.exit(1);
}
}
}

Tutor Answer

(Top Tutor) Daniel C.
(997)
School: Boston College
PREMIUM TUTOR
Studypool has helped 1,244,100 students
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1824 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors