There are two text files, whose names are given by two String variables, file1 and file2. These text files 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. When finished, make sure that the data written to the new file has been flushed from its buffer 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.)

There are two text files, whose names are given by two String variables, file1 and file2. These text files 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. When finished, make sure that the data written to the new file has been flushed from its buffer 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.)

LANGUAGE: JAVA

CHALLENGE:

There are two text files, whose names are given by two String variables, file1 and file2. These text files 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. When finished, make sure that the data written to the new file has been flushed from its buffer 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.)

SOLUTION:

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

Add comment