Java Program to Merge Two Files

This article is created to cover a program in Java that merge the content of two files into the third file, entered by user at run-time of the program.

What to Do before Program ?

Since the program given below merges the content of two files into the third file. Therefore, we need to create two files with some content, to merge the content of these two files into a third file using the Java program.

If the third file, in which the merged content is going to write, is not available, inside the current directory. Then a new file with the name provided as the name of third file, will get created. And if the file is already available, then the merged content will get appended into the file. Here is the snapshot of the current directory, including three opened files, in my case:

java program merge two files

Using the sample run of the program given below, the content of codes.txt and cracker.txt file will get merged into the codescracker.txt file, because I'm going to provide the name of these files.

Merge Content of Two Files into Third File in Java

The question is, write a Java program to merge the content of two files into a third file. The name of all the three files must be received by user at run-time of the program. The program given below is its answer:

import java.io.*;
import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      String fileOne, fileTwo, fileThree, line, content="";
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Name of First File: ");
      fileOne = scan.nextLine();
      System.out.print("Enter the Name of Second File: ");
      fileTwo = scan.nextLine();
      System.out.print("Enter the Name of Third File: ");
      fileThree = scan.nextLine();
      try
      {
         FileReader frOne = new FileReader(fileOne);
         BufferedReader brOne = new BufferedReader(frOne);
         FileReader frTwo = new FileReader(fileTwo);
         BufferedReader brTwo = new BufferedReader(frTwo);
         
         for(line=brOne.readLine(); line!=null; line=brOne.readLine())
            content = content + line + "\n";
         brOne.close();
         
         for(line=brTwo.readLine(); line!=null; line=brTwo.readLine())
            content = content + line + "\n";
         brTwo.close();
         
         try
         {
            FileWriter fw = new FileWriter(fileThree, true);
            fw.write(content);
            fw.close();
            System.out.println("\nSuccessfully merged the content of two files into the third file");
         }
         catch(IOException ioe)
         {
            System.out.println("\nSomething went wrong!");
            System.out.println("Exception: " +ioe);
         }
      }
      catch(IOException ioe)
      {
         System.out.println("\nSomething went wrong!");
         System.out.print("Exception: " +ioe);
      }
   }
}

The snapshot given below shows the sample run of above program with user input codes.txt as name of first, cracker.txt as name of second, and codescracker.txt as name of third file, to merge the content of first two files into the codescracker.txt file:

merge two files in java

Now if you open the file codescracker.txt, then the file contains the content of first and second file. Here is the new snapshot of the same directory, with all three files:

merge two files java program

The program to merge two files into third, in Java, can also be written as:

import java.io.*;
import java.util.Scanner;

public class CodesCracker
{
   public static void main(String[] args)
   {
      String fileOne, fileTwo, merge;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("Enter the Name of First File: ");
      fileOne = scan.nextLine();
      System.out.print("Enter the Name of Second File: ");
      fileTwo = scan.nextLine();
      
      System.out.print("Enter the Name of Third File: ");
      merge = scan.nextLine();
      
      File[] files = new File[2];
      files[0] = new File(fileOne);
      files[1] = new File(fileTwo);
      File mergedFile = new File(merge);
      
      mergeFiles(files, mergedFile);
   }
   public static void mergeFiles(File[] files, File mergedFile)
   { 
      FileWriter fstream = null;
      BufferedWriter out = null;
      try
      {
         fstream = new FileWriter(mergedFile, true);
         out = new BufferedWriter(fstream);
      }
      catch(IOException e1)
      {
         System.out.println("Exception: " +e1);
      }
      System.out.println("\nMerging the file...");
      for(File f: files)
      {
         FileInputStream fis;
         try
         {
            fis = new FileInputStream(f);
            BufferedReader in = new BufferedReader(new InputStreamReader(fis));
            String aLine;
            while((aLine = in.readLine()) != null)
            {
               out.write(aLine);
               out.newLine();
            }
            in.close();
         }
         catch(IOException e)
         {
            e.printStackTrace();
         }
      }
      System.out.println("\nMerged Successfully!");
      try
      {
         out.close();
      }
      catch(IOException e)
      {
         System.out.println("Exception: " +e);
      }
   }
}

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »


Liked this post? Share it!