Android Question Fixed size records in RandomAccessFile


Licensed User
Longtime User
I have a problem creating records of the same size in RandomAccessFile.

My objective is to read/write individual record in a RandomAccessFile by its order in the file.
For example, to read the 5th record, the command would be ReadObject(4 * RecordSize), where RecordSize is a constant.
The record has the following fields.

Type REC_TYPE( _
    Id(5) As  Byte, _   
    Value As Double _ 

Although the fields have fixed size, I notice that the records stored in RandomAccessFile have different sizes depending on the actual content of Id although Id is declared as 5 bytes.

My question:
One solution for me is to append filler so all records are the same size.
To do that I must find out what is the maximum possible record size (compressed or not) given the fields I have.
Can the record size be calculated ?

(Note: SQL is not suitable for my application).


B4X founder
Staff member
Licensed User
Longtime User
Don't use ReadObject / WriteObject for fixed records. It is not efficient and there are too many parameters that you don't control.

Instead create a simple method that writes your type:
Sub WriteRec(rec As REC_TYPE, raf As RandomAccessFile)
 raf.WriteBytes(rec.Id, 0, rec.Id.Length, raf.CurrentPosition)
 raf.WriteDouble(rec.Value, raf.CurrentPosition)
End Sub
You will need to implement a similar sub that reads the data and creates a new type.
Upvote 0