WAP to merge data of 2 files into another file

Let’s write a Java Program to merge two files into a single file. Here we wll use java BufferedReader class for reading two files and PrintWriter class for write file content in final file.

Java Program

package file;

import java.io.*;

public class FileMerge {
    public static void main(String[] args) throws IOException {
        PrintWriter pw = new PrintWriter("mergedFile.txt");
        BufferedReader br = new BufferedReader(new FileReader("input1.txt"));
        String line;
        while ((line= br.readLine())!=null){
            pw.println(line);
        }
        br = new BufferedReader(new FileReader("input2.txt"));
        while ((line= br.readLine())!=null){
            pw.println(line);
        }
        pw.flush();
        pw.close();
        br.close();
    }
}

See Also

Leave a Comment