top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

How to merge two files into a third file in java?

+1 vote
139 views
How to merge two files into a third file in java?
posted Jul 7, 2017 by Ajay Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

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");
    }
}
answer Jul 7, 2017 by Salil Agrawal
Similar Questions
0 votes

I need to write into a file for a project which will be evaluated on the basis of time. What is the fastest way to write 200 Mb of data, accumulated as a list into a file.

Presently I am using this:

with open('index.txt','w') as f:
 f.write("".join(data))
 f.close()

where data is a list of strings which I want to dump into the index.txt file

0 votes

Has anyone archived some third-party libs like jars, .so files into a module to generate an aar file? I put the .so files into jniLibs, but when I generate an aar files, I found the menu of the files was wrong with some empty dirs. And I put this aar file into my project, something goes wrong (crashes) when it calls .so files.

Can u help me to make it right?

+3 votes

If you use fread to read a file of unknown size with a set buffer size of say 500 bytes, when you get to the end of the file you are likely to read a stub of 500 bytes of info and the eof marker.

And fread will not read that block and set and oef that can be read with feof. That is how I understand, so we can either use fseek to discover the file size and fread accordingly or use fstat?

Is this the proper approach?

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...