aspose file tools*
The moose likes Java in General and the fly likes Help Parsing a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help Parsing a file" Watch "Help Parsing a file" New topic

Help Parsing a file

kalyan enganti

Joined: Dec 12, 2011
Posts: 6
I have got a file( datafile.dat) which is a collection of barcodes and their quantities got from a hand held terminal.the barcode is 64 bytes long followed by a space and then followed by the quantity.The next record starts at the 66th bytewith a length 64 bytes and followed by a space and the barcode and so on.I have a problem in parsing the data .The sample input file has 11 records .The first nine records are obtained as expected,but I get a String index out of bounds while trying to read the 10 record.I have made an assumption that each record is 66 bytes long. Also the bufferedreader shows the record length as 658 bytes but the file length is 726 bytes,is that normal?.I am attaching the input file for better clarity.Any pointer in the right direction would be greatly appreciated.

link to input file

The output of the file run:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 658
at java.lang.String.charAt(
at Reader.main(
record length 658, no of records 9
File length :726
Java Result: 1
Tim Moores

Joined: Sep 21, 2011
Posts: 2408
Not sure what the code is doing, but if a string has 658 characters then the last character is at position 657.
kalyan enganti

Joined: Dec 12, 2011
Posts: 6
Hi Tim
Thanks for getting back.
The file structure is as follows
1-64 bytes barcode
65 empty space
66 byte-quantity
67-131 bytes barcode
132 empty-space
133-197-bytes barcode
198-emtpy space
199- quantity and so on so forth.
Please find the attached file for more info.

I have taken 66 bytes as each record length and I am trying to figure out the the number of records from record.length/66. extract the barcode and the quantity strings from them.Also the buffered reader gives out a null before all the records are read.I hope my EOF check is right.The file length is 726 bytes but the buffered reader gives out an eof at the 658 byte. I am wondering where I am going wrong.

Tim Moores

Joined: Sep 21, 2011
Posts: 2408
It would seem that the file contents span more than a single line. If you want to use indexes that cover the full file length, then you need to concatenate all of the file contents together into a single string first.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12783
Why are you using readLine when the data is in fixed length records with no line termination??

The readLine method is for character text organized as lines. Anyway, you should not be using a Reader, binary data will just confuse it.

If you know the record layout, why not use that information to read a single record at a time into a byte[]. For example, with


adam smith ii

Joined: Feb 04, 2010
Posts: 20
What is occurring is that there are at least 2 record sets. The first record set has 9 records.

I added a display to the do loop and here is the output and changed the precision of the math:
kalyan enganti

Joined: Dec 12, 2011
Posts: 6
Thanks a bunch folks .Thanks for all your inputs.There was a line with all nulls in between which was causing the readline to fail.RandomAccessFile was what I needed.I ahve modified the code to use Random AccessFile and it s working as expected.

I agree. Here's the link:
subject: Help Parsing a file