Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that opens two text files (input1.txt and input2.txt) for input

ID: 3914477 • Letter: W

Question

Write a program that opens two text files (input1.txt and input2.txt) for input and one for output (output.txt). The program should merge the two input files in the output files. While doing the merge, it will read one line from input1.txt and one line from input2.txt and write those in the output file one after other. Then it will read the next line from input1.txt and next line from input2.txt and write them one after other in the output file. Here is an example. input1.txt: This is the first line in input1.txt. This is the second line in input1.txt. This is the third line in input1.txt. This is the fourth line in input1.txt. input2.txt: This is the first line in input2.txt. This is the second line in input2.txt. This is the third line in input2.txt. outtput.txt This is the first line in input1.txt. This is the first line in input2.txt. This is the second line in input1.txt. This is the second line in input2.txt. This is the third line in input1.txt. This is the third line in input2.txt. This is the fourth line in input1.txt. After the writing in output.txt, count the number of lines in output.txt. Also, write the number of times term ‘input1’ appears in output.txt file.

Explanation / Answer

Since you have not provided the language of your preference, I am providing the code in Java.

CODE

================

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

public class tester

{

   public static void main(String[] args) throws IOException

   {  

       String source1 = "input1.txt";

       String source2 = "input2.txt";

       String dest = "output.txt";

       File fin1 = new File(source1);

       FileInputStream fis1 = new FileInputStream(fin1);

       File fin2 = new File(source2);

       FileInputStream fis2 = new FileInputStream(fin2);

       BufferedReader in1 = new BufferedReader(new InputStreamReader(fis1));

       BufferedReader in2 = new BufferedReader(new InputStreamReader(fis2));

       FileWriter fstream = new FileWriter(dest, true);

       BufferedWriter out = new BufferedWriter(fstream);

      

       int count= 0;

       String find = "input1";

       String aLine = null, bLine = null;

       while ((aLine = in1.readLine()) != null && (bLine = in2.readLine()) != null) {

           count += aLine.split(find, -1).length - 1;

           count += bLine.split(find, -1).length - 1;

           out.write(aLine);

           out.newLine();

           out.write(bLine);

           out.newLine();

       }

      

       if(aLine != null) {

           count += aLine.split(find, -1).length - 1;

           out.write(aLine);

           out.newLine();

       }

      

       if(bLine != null) {

           count += bLine.split(find, -1).length - 1;

           out.write(bLine);

           out.newLine();

       }

      

       while ((aLine = in1.readLine()) != null) {

           count += aLine.split(find, -1).length - 1;

           out.write(aLine);

           out.newLine();

       }

      

       while ((bLine = in2.readLine()) != null) {

           count += bLine.split(find, -1).length - 1;

           out.write(bLine);

           out.newLine();

       }

      

       out.write("Number of times term ‘input1’ appears = " + count);

      

       // do not forget to close the buffer reader

       in1.close();

       in2.close();

       // close buffer writer

       out.close();

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote