aspose file tools*
The moose likes Beginning Java and the fly likes How to merge .bin files? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to merge .bin files?" Watch "How to merge .bin files?" New topic
Author

How to merge .bin files?

Michelle Gonzalez
Greenhorn

Joined: Aug 06, 2012
Posts: 5
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

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4527
    
    5

As this question has nothing to do with Swing / AWT / SWT, I'm moving it to Beginning Java.


luck, db
There are no new questions, but there may be new answers.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
Michelle Gonzalez
Greenhorn

Joined: Aug 06, 2012
Posts: 5
Yes I want to concatenate the files. I have changed to this but it gives me the same issue.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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.
Michelle Gonzalez
Greenhorn

Joined: Aug 06, 2012
Posts: 5
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


[Thumbnail for temp.png]

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Curses, I've been Junilued.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Mike Simmons wrote:Curses, I've been Junilued.

Yikes, I have been verbified
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to merge .bin files?