Ada
Ada

Reputation: 624

How to merge binary files using Java?

I am downloading file parts using socket, saving them as .part1, .part2 etc.( However I am not sure if I should). I am trying to merge them in a .bin file. Can any of you recommend me a way to do that? Should I read them all as binary and then append it? I don't even know if that makes sense actually.

Upvotes: 1

Views: 4848

Answers (2)

John R Doner
John R Doner

Reputation: 2282

Why don't you open the first part for input, and open an output streamof the form

out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)));

then read in your first part file as bytes, and use the writeByte() method to write it all out.

Then close the first part, open the second part, and read it in, then out to the output file.

Will that not merge the contents of the two files?

Upvotes: 2

Ada
Ada

Reputation: 624

I did it. Read files one by one and wrote them to output.

Upvotes: 0

Related Questions