Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions