Programming with the RMS (Cont.)


Reading Data from the Record Store
To read a record from the record store, you construct input streams instead of output streams. The getRecord method reads the contents of a record.
byte[ ]  data = new byte[100];
int      id = ....;    // get the ID from somewhere
try {
  int  numBytes = rs.getRecord( id, data, 0 );
}
catch( ArrayIndexOutOfBoundsException e ) {
  // record too big for the array
}
catch( InvalidRecordIDException e ) {
  // record doesn't exist
}
catch( RecordStoreNotOpenException e ) {
  // store has been closed
} 
catch( RecordStoreException e ){
  // general error
}
Deleting a Record from the Record Store
To delete a record from the record store, you have to know the record ID for the record to be deleted. The deleteRecord( ) method takes an integer as a parameter, which is the record ID of the record to be deleted. There is no method to get the record ID. To work around this, every time you create a new record, add its record ID to a vector like this:
Vector  recordIDs = new Vector( );
int  lastID = 1;
//Add a record....parameters are missing here
db.addRecord( );
// Now add the ID to the vector
recordIDs.addElement( new Integer(++lastID) );
Now, to delete a record, find the record ID of the record you want to delete:
Enumeration  IDs = recordIDs.elements( );
while( IDs.hasMoreElements( ) ) {
  int  id = ((Integer) IDs.nextElement( )).intValue( );
  // Compare to see if this is the record you want by
  // invoking compare( ) which is shown next.
  // Then call db.deleteRecord( id );
}