Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Friday, 12 January 2018

Java program to merge two files

This 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.

                                PROGRAM




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();
        }
    }
}

No comments:

Post a Comment