L leongcc Member Licensed User Longtime User Sep 10, 2014 #1 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. Code: 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).
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. Code: 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).
Erel B4X founder Staff member Licensed User Longtime User Sep 10, 2014 #2 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: Code: 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
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: Code: 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.