Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struggling to read in a binary file correctly

 
Christopher Turner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a binary file that has identifiers for the start of each record



You can see here that the record starts with 00 00 and the next record starts with 00 00 etc...

How do I read between these two points throughout the file?



The first start would be the start of the file, the next start would be the end of the first start and so on..

I'm just not sure how to put that into a loop


 
Tony Docherty
Bartender
Posts: 3054
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.

Why are you using RandomAccessFile if you are sequentially reading the file or have I mis-understood what you are doing?
Are the blocks of data a fixed length or do you they vary in length in which case you need to read until you find the next 00 00 bytes which could be a problem as the data shown contains 00 00 embedded in it so how would you tell them from the start of the next block
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the setLength() method truncates the file. And in particular setLength(0) sets the length of the file to zero. That makes it kind of pointless to continue with code which tries to read the file, since it's now empty. Are you sure you're on the right track with using RandomAccessFile?
 
Christopher Turner
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry this is a case of me not correctly articulating what I'm trying to achieve.

Let me start again:

The file holds records, each record has a number of label but is of random length. Each record is separated by a two digit byte, represented in the hex table as 00 00

I'm just unsure how to read the file to find those separators. (Do I even need to do this?)

I thought I would be able to calculate the length of each record and read out the bytes for each value but that didn't seem to work.

The issue is the output seems to be all over the place and values don't correctly line up. The name might hold the size, and the rate value might hold the location for example.


And from the Contract class


Edit: After more and more googling it seems I'm trying to do something similar to this gentleman over on Stackoverflow http://stackoverflow.com/questions/10899509/is-there-any-efficient-method-to-find-the-first-byte-of-all-instances-of-a-parti

But I don't (and can't) have access to ByteUtils...


Edit number 2:

After more research I've come up with how I can find the flag in the file.



This really doesn't feel right though.

This outputs (sample)


Just not sure where to go from this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic