codescracker


java

Java Program to Merge Two Files



« Previous Program Next Program »


Merge Two Files

To merge two files in Java Programming, You have to ask to the user to enter the name of both the files with extension to merge its content and store the merged content in the third file so also ask to the user to enter the third file name with extension to perform this action as shown in the following program.

Java Programming Code to Merge Two Files

Following Java Program ask to the user to enter the first and second file name with extension to merge its content and then ask to the user to enter the third file name with extension to store the merged content inside it:

/* Java Program Example - Merge Two Files */
		
import java.io.*;
import java.util.Scanner;

public class JavaProgram
{ 
    public static void main(String args[])
    {	
	
        String srcy, srcz, merge;
        Scanner scan = new Scanner(System.in);
        
        /* enter the file names with extension like file.txt */
		
        System.out.print("Enter First File Name : ");
        srcy = scan.nextLine();
        System.out.print("Enter Second File Name : ");
        srcz = scan.nextLine();
		
        System.out.print("Enter FileName to Store merged content of First and Second File : ");
        merge = scan.nextLine();
 
        File[] files = new File[2];
        files[0] = new File(srcy);
        files[1] = new File(srcz);
 
        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)
        {
            e1.printStackTrace();
        }
		
        System.out.print("Merging Both File...\n");
		
        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.print("\nMerged Successfully..!!");
 
        try
        {
            out.close();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

When the above Java Program is compile and executed, it will produce the following output:

Java Program to merge two files

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test