Hi everyone! So Im trying to merge .bin files into one .bin file. These .bin files are images I have tried many ways to do it my process now is to merge the images into a new file created. The problem is that only one file copies over but does not merge the other ones. Can someone please help me or give me ideas about how to go about it? Thanks, Michelle
If you want to append data to a file, instead of overwriting it, then use the constructor of FileOutputStream which says you want to do that. (And not the one you used in that code.) Check the API documentation and read about the available constructors.
Of course if "merge" means something other than just concatenating the images, then you're going to have to do something completely different. Depending on what "merge" means.
Are you sure it's exactly the same issue? If only one file seems to be copied, which one is it? The first file, the last file, or some other? And, how does the length of the resulting file compare to the lengths of the other files? Can you show a list of file lengths?
One problem I see with your code is the out.write() needs to be told how many bytes to write. With the code you've got, it will write the entire buffer every time. However it's possible that the buffer wasn't actually filled, in which case you will get some other data - zeroes or old data - written instead. You need to use the len variable when you write, as well as when you read.
It copies the first file only. The size of the new file is correct meaning it has the size of the file when all the files are merge into. I tried to put the len in the write and it still just copies the first page only. I attached a file with what the files sizes looks like
Strange. I would add some print statements for debugging. Each time you open a new file in the loop, print the name of the file you've opened, and what size it has. Make sure it's really reading all the files you think it should be reading. Also, is it possible there's an exception being thrown and you haven't noticed the stack trace? (That may seem obvious, but in some environments, it may not be.) And just for good measure, put the close() statements inside finally blocks. And there's no use for a flush() after a close() - just drop it. The close() will include a flush() automatically anyway. And if it didn't, you would want to do the flush() before the close(), not after.
if the resulting file has the expected size (total of all the file sizes being "merged"), then the problem might be something else. What makes you think that only the first file is copied if the resulting file size is what you expect? Are you trying to view the resulting BIN file with another program and it's only showing you the contents of the first file? If so, then maybe the "merged file" has some header bytes that the viewer uses to determine how to process the rest of the file and naturally only sees the header bytes for the first file. Maybe your "merge" operation is really not the correct way to merge these kinds of files together.
Michelle Gonzalez wrote:The size of the new file is correct meaning it has the size of the file when all the files are merge into.
Oh, wait, in this case it seems quite possible that the copying is working fine. The problem may be that whatever you're using to read the file does not understand that your file is intended to be multiple files concatenated together. In fact I don't know that any standard image format does support this. Are you sure concatenating images is a good idea in the first place? Do you have any working examples where you can open the file and see multiple images?