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