We want to write three objects to a file. However we want to be able to skip to the third object directly. So we write the position of the third object at the beginning of the file, this allows us later to read the position and jump to the third object directly.
B4X:
Sub WriteToFile (o1 As Object, o2 As Object, o3 As Object)
Dim raf As RandomAccessFile
raf.Initialize(...)
raf.CurrentPosition = 4
raf.WriteObject(o1, True, raf.CurrentPosition)
raf.WriteObject(o2, True, raf.CurrentPosition)
raf.WriteInt(raf.CurrentPosition, 0)
raf.WriteObject(o3, True, raf.CurrentPosition)
raf.Close
End Sub
Note that in most cases it is simpler and easier to use KeyValueStore class.
When you write the position of the end of 02 in the begining of the file, you have to store this point in a variable and then write
raf.WriteObject(o3, True, [the value])
because raf.CurrentPosition now is 4 and o3 will write over o1 and o2.