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.
The output of the file run:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 658
record length 658, no of records 9
File length :726
Java Result: 1
Not sure what the code is doing, but if a string has 658 characters then the last character is at position 657.
Joined: Dec 12, 2011
Thanks for getting back.
The file structure is as follows
1-64 bytes barcode
65 empty space
67-131 bytes barcode
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.
Joined: Sep 21, 2011
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.
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:
Joined: Dec 12, 2011
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.