Question: Program in Java There are two text files, whose names are given by two String variables, file1 and file2 . These text files may NOT

Program in Java

There are two text files, whose names are given by two String variables, file1 and file2. These text files may NOT have the same number of lines. Write a sequence of statements that creates a new file whose name consists concatenating the names of the two text files (with a "-" in the middle) and whose contents of merging the lines of the two files. Thus, in the new file, the first line is the first line from the first file, the second line is the first line from the second file. The third line in the new file is the second line in the first file and the fourth line is the second line from the second file, and so on. If, as is likely, one file runs out first stop writing to new file and create another new file called "leftovers". Copy the remaining lines that have not been written out that into "leftovers". When finished, make sure that the data written to the new files have been flushed from their buffers and that any system resources used during the course of running your code have been released.(Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.)

Modify code below to solve:

File f1 = new File(file1); File f2 = new File(file2); Scanner scan1 = new Scanner(f1); Scanner scan2 = new Scanner(f2); String sFile3 = file1 + "-" + file2; File file3 = new File(sFile3); PrintWriter output = new PrintWriter(file3); while(scan1.hasNextLine()){ output.println(scan1.nextLine()); output.println(scan2.nextLine()); } output.close(); output.close();

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!