Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to merge .bin files?

 
Michelle Gonzalez
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this question has nothing to do with Swing / AWT / SWT, I'm moving it to Beginning Java.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I want to concatenate the files. I have changed to this but it gives me the same issue.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
temp.png
[Thumbnail for temp.png]
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7594
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Curses, I've been Junilued.
 
Junilu Lacar
Bartender
Posts: 7594
53
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Curses, I've been Junilued.

Yikes, I have been verbified
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic