Java Program to merge two files in a third file

In this tutorial, we are going to learn how to merge two files using Java Program.

To merge the two files into the third file firstly, we need to create two files namely f1 and f2.
We should create the files f1 and f2 in the same folder.

Merge two files in Java

Let us assume that the contents of files as:
file1 contains: hello all !!

file2 contains: learn programming from codespeedy .
After merging the two files the file3 contains: hello all !! learn programming from codespeedy.

Here is the Java program.

import java.io.*;
import java.util.Scanner;
public class MergeProgram
{ 
 public static void main(String args[])
    {	
     String file1, file2, file3;
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter file1 : ");
        file1 = scan.nextLine();
        System.out.print("Enter file2 : ");
        first2 = scan.nextLine();
        System.out.print("Enter the third file  : ");
        file3 = scan.nextLine();
 
        File[] files = new File[2];
        files[0] = new File(file1);
        files[1] = new File(file2);
        File mergedF3 = new File(file3);
        mergeFiles(files, mergedF3);
    
         }
 
    public static void mergeFiles(File[] files, File mergedF3)
    { 
        FileWriter fstream = null;
        BufferedWriter out = null;
    
        try
        {
            fstream = new FileWriter(mergedF3, true);
            out = new BufferedWriter(fstream);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    
        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 e1)
            {
               e1.printStackTrace();
            }
        }
        System.out.print("\nTwo files are succesfully merged into the third file.");
 
        try
        {
            out.close();
        }
        catch(IOException e1)
        {
            e1.printStackTrace();
        }
    }
}

The above code will give the following output.
Assume that file1 contains “hello all!!”.
File2 contains “learn programming from codespeedy.” after merging the file1 and file2 into file3.
The code gives the output as “two files are successfully merged into the third file.”
When we open the file3 it contains”hello all!! learn programming from codespeedy.”

Enter file1:  file1.txt
 Enter file2: file2.txt
 Enter the third file: file3
 Two files are successfully merged into the third file.

 

Also, read Fetch all email id from a text file

Leave a Reply

Your email address will not be published. Required fields are marked *