There are various ways simplest would be use of system command of linux within the Runtime.getRuntime().exec().
Linux Command -
cat file1 >> file3
cat file2 >> file3
Run the above two commands within the Runtime.getRuntime().exec() one after another -
Runtime.getRuntime().exec(new String[]{"bash","-c","cat file1 >> file3"});
Runtime.getRuntime().exec(new String[]{"bash","-c","cat file2 >> file3"});
Now Assume you want to run the Java code then read line by line from one file and write the same at the end of the third file. Repeat the same for the second file also. (found the following sample implementation on geekforgreeks)
import java.io.*;
public class FileMerge
{
public static void main(String[] args) throws IOException
{
// PrintWriter object for file3
PrintWriter pw = new PrintWriter("file3");
// BufferedReader object for file1
BufferedReader br = new BufferedReader(new FileReader("file1"));
String line = br.readLine();
// loop to copy each line of
// file1 to file3
while (line != null)
{
pw.println(line);
line = br.readLine();
}
br = new BufferedReader(new FileReader("file2"));
line = br.readLine();
// loop to copy each line of
// file2 to file3
while(line != null)
{
pw.println(line);
line = br.readLine();
}
pw.flush();
br.close();
pw.close();
System.out.println("Merged file1 and file2 into file3");
}
}