• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem reading the database file (Help please)

 
efoe akolly
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I successfully read the header of the file and I can also read the first record without a problem. But for some reason I am unable to read the second record and so on. I get an Exception (ArrayOutOfBoundException) telling me that my pointer is pointing to a certain index.
Here is the code snippet. I tried everything but it won't work. I start to wonder if the file Sun gave me is right. Please help me out.
Thanks

/**
* Reads a record from the file. Returns an array where each element is a
* record value.
*
* @param recNo
* the record number
*/
public String[] read(int recNo) throws RecordNotFoundException {
byte[] record = new byte[recordLength];
String[] recordValues = null;
int offset = 1; // for flag
try{
seek(recNo);
raf.read(record);
byte validityFlag = record[0];
if(validityFlag == ACTIVE_RECORD ) {
recordValues = new String[fields.length];
for(int currentField = 0; currentField < fields.length; currentField ++){
recordValues[currentField] = new String(record,offset,fields[currentField].getFieldLength());
offset += fields[currentField].getFieldLength();
}
}

/*
// for debugging purpose
for(int i = 0; i < recordValues.length; i ++){
System.out.println(recordValues[i]);
}
*/
return recordValues;

}
catch(IOException e){
e.printStackTrace();
}
return null;
}
 
efoe akolly
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind. I just figured it out. I forgot to add the validity flag to the record length read earlier.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic