----- Original Message -----
From: "bejay" <
[email protected]>
To: <
[email protected]>
Sent: Saturday, December 06, 2003 1:28 AM
Subject: Re: How to determine integer values in a txt file?
Baj,
<original text>
Sorry for butting into this thread, but the initial question that was posed
here is something that I am having trouble with too, and you seem to be
somebody well versed in the art of Java... unlike myself I hasten to add.
I have read a few books, but need more examples to better understand the
code and thought if I can write a little programme I would be able to
understand and build on this.
I have written a basic programme that basically accepts a binary file and
creates an output file. Nothing much in between, a few Exception Catches,
but so far it was a little exercise to have input and output selected from
the command line.
I would like to do similar to the question below but not sure where to start
and what the syntax should/would be.
The task:
From any given binary input file, which has a fixed header for example in
hex 01,02,03. I would like my utility to search the input file, lock onto
the fixed header, skip 2 bytes and read the 5th byte to determine how many
bytes to output to my new file?
For example if the 5th byte was in hex 32 (Decimal 50) then take the next 50
bytes and output this to my new file?
Is there an easy way of doing this?
</original text>
The problem boils down to one of positioning and then reading - how it can
best be accomplished depends on how the data is stored.
You say the data is in binary. Sadly, there is no such thing as a
'universal' binary format, so, if your data file was generated by a non-Java
program, maybe on another platform [i.e. different processor / operating
system], you could experience difficulties with different byte orders, or
different floating point formats [if applicable]. For the current
discussion, we'll assume no such problems exist.
For this problem the most appropriate stream would seem to be a
'FileInputStream', optionally wrapped in a 'BufferedInputStream'. However,
it is also possible to use a 'RandomAccessFile', particularly useful if
arbitrary file pointer movement is needed.
Before describing the location / read process in detail, it is worth
mentioning that had the data been generated by a 'DataOutputStream',
creating a 'standardised' type of binary data, a 'DataInputStream' would
have been used to read the data. The biggest benefit of doing so would have
been the many convenient methods for reading data such as 'readInt', and
'readDouble' etc. It goes without saying that if it is possible to control
the format data will be stored in much can be done to make its procssing
convenient and efficient.
Ok, now to the problem at hand. If the fixed-size header is at the start of
the stream then the task is easy - just read the header in ! If the
fixed-size header is in an arbitrary location you need to search the stream
for the header's signature [i.e. a sequence of bytes acting as a magic
number or identifier]. I see no other means than a byte-by-byte read [e.g.
if 1st byte found, check for 2nd, and 3rd etc - either a signature is found,
or you simply keep searching and testing].
In either case, having found the header, skipping bytes can be accomplished
via 'read' [and discarding the unneeded bytes], or via 'skip'. Reading in
the number of bytes sees a single 'read' performed, while reading in the
data can probably best be accomplished by reading into a 'byte' array.
For writing the 'byte' array to a text file [previously opened using a
'FileWriter' ?] just wrap it up in a 'String' [though note there may be
problems decoding byte -> char], and use the 'FileWriter's' 'write(String,
offset, len)' method.
Barring binary incompatibilty / byte decoding problems, this task is,
really, quite easy to accomplish. The best of luck in your endeavours
!
Cheers,
Anthony Borla