Create a MergeFiles application that merges the integers ord

Create a MergeFiles application that merges the integers ordered from low to high in two text data files into a third text data file, maintaining the order from low to high (no post merge sorts!). For example, the two files of integers could contain:

File 1: 12 23 34 45 56 67 69 123 133

File 2: 4 5 10 20 35 44 100 130 150 160 180

The application should not use an array or ArrayList to temporarily store the numbers, but should merge the two files taking one element at a time from each. After MergeFiles runs for this example, the third file should contain:

4 5 10 12 20 23 34 35 44 45 56 67 69 100 123 130 133 150 160 180

Create the two input files using a text editor with the numbers of your choice. Be sure to employ proper exception handling for the file I/O. Use at least one try-catch statement that properly handles exceptions.

Solution

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class MergeFiles {

   public static void main(String[] args) throws IOException
   {
       Scanner sc1 = null;
       try {
           sc1 = new Scanner(new FileReader(\"file1.txt\"));
       } catch (FileNotFoundException e) {
           System.out.println(\"No file1.txt found.\");
       }
      
       Scanner sc2 = null;
       try {
           sc2 = new Scanner(new FileReader(\"file2.txt\"));
       } catch (FileNotFoundException e) {
           System.out.println(\"No file2.txt found.\");
       }
      
       FileWriter writer = null;
       try {
           writer = new FileWriter(\"output.txt\");
       } catch (IOException e) {
           System.out.println(\"Unable to create file. Exiting.\");
           return;
       }
      
       if (sc1 == null && sc2 == null)
       {
           return;
       }
      
       if (sc1 != null && sc2 == null)
       {
           while(sc1.hasNextInt())
           {
               writer.write(sc1.nextInt()+ \" \");
           }
           sc1.close();
           writer.close();
           return;
       }
      
       if (sc2 != null && sc1 == null)
       {
           while(sc2.hasNextInt())
           {
               writer.write(sc2.nextInt()+ \" \");
           }
           sc1.close();
           writer.close();
           return;
       }
      
       int first = 0;
       int second = 0;
       boolean shouldFirst = true;
       boolean shouldsecond = true;
       while(sc1.hasNextInt() && sc2.hasNextInt())
       {
           if (shouldFirst)
               first = sc1.nextInt();
           if (shouldsecond)
               second = sc2.nextInt();
           if (first < second)
           {
               writer.write(first + \" \");
               shouldFirst = true;
               shouldsecond = false;
           }
           else
           {
               writer.write(second + \" \");
               shouldFirst = false;
               shouldsecond = true;
           }
       }
      
       while(sc1.hasNextInt())
       {
           writer.write(sc1.nextInt() + \" \");
       }
      
       while(sc2.hasNextInt())
       {
           writer.write(sc2.nextInt() + \" \");
       }
       writer.close();
   }
}

Create a MergeFiles application that merges the integers ordered from low to high in two text data files into a third text data file, maintaining the order from
Create a MergeFiles application that merges the integers ordered from low to high in two text data files into a third text data file, maintaining the order from

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site