Java Program To Merge Two Files

Chapter: File Last Updated: 28-10-2016 13:27:14 UTC

Program:

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

public class JavaMergeTwoFiles {

	public static void main(String args[]) {

		String srcy, srcz, merge;
		Scanner scanner = new Scanner(System.in);

		/* enter the file names with extension like file.txt */

		System.out.print("Enter First File Name : ");
		srcy = scanner.nextLine();
		System.out.print("Enter Second File Name : ");
		srcz = scanner.nextLine();

		System.out.print("Enter FileName to Store merged content of First and Second File : ");
		merge = scanner.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();
		}
	}
}
                /* ............... END ............... */
        

Tags

Java Program To Merge Two Files, Java, File

Similar Programs Chapter Last Updated
Java Sort Files By Size File 07-06-2017
Java Sort Files And Directories By Name File 07-06-2017
Java Append Text To A File File 25-03-2017
Java Program To Set File Permission File 13-02-2017
Java File Modification Time File 25-10-2016
Java Read File File 25-10-2016
Java Print Directory Hierarchy File 03-09-2016
Java Hidden Directory Example File 03-09-2016
Java Empty Directory Example File 03-09-2016
Java Delete Directory Example File 03-09-2016
Java New File Creation Example File 03-09-2016
List All File Names From Folder In Java Example File 20-06-2016
Current Working Directory In Java File 17-06-2016
Create Directory In Java Example File 17-06-2016
Determine File Or Directory In Java File 22-07-2016
Delete File In Java File 22-07-2016
Java Temporary File File 22-07-2016
Compare Two File Paths In Java File 17-04-2016
Create File In Java File 17-04-2016
PrintWriter In Java Example File 18-07-2016
FileWriter In Java Example File 18-07-2016
Write To A File In Java Example File 06-04-2016
File Size In Java Example File 16-06-2016
File Rename In Java Example File 16-06-2016
Read Only File In Java Example File 16-06-2016
File Path Comparison In Java Example File 15-07-2016
Copy File To Another File In Java Example File 20-06-2016
File Modification Date In Java Example File 13-06-2016
File Exists Or Not In Java Example File 13-06-2016
File Creation In Java Example File 15-07-2016

1 2