Yes.
B4X:raf.WriteB4XObject(o, raf.Size)
For d = 0 To (l.Size - 1)
m = l.Get (d)
tmpthreadid = m.Get ("threadid")
RAF.Initialize2(File.DirDefaultExternal & "/" & usernametmp & "/messages/" , tmpthreadid, False,False)
RAF.WriteB4XObject(l.Get (d),RAF.size)
DoEvents
Log ("incomging " & l.Get (d))
Log ("file " & RAF.ReadB4XObject(0))
Next
incomging {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=92337321312222016034304, msgtype=}
file {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=92337321312222016034304, msgtype=}
incomging {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=32629016712222016034323, msgtype=}
file {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=32629016712222016034323, msgtype=}
incomging {timestamp=2016-12-21 14:43:23, message=DEF, sender=kk, username=, threadid=32629016712222016034323, msgtype=}
file {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=32629016712222016034323, msgtype=}
incomging {timestamp=2016-12-21 14:43:47, message=GHI, sender=ll, username=, threadid=32629016712222016034323, msgtype=}
file {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=32629016712222016034323, msgtype=}
incomging {timestamp=2016-12-21 14:44:05, message=JKL, sender=kk, username=, threadid=92337321312222016034304, msgtype=}
file {timestamp=2016-12-21 14:43:04, message=ABC, sender=ll, username=, threadid=92337321312222016034304, msgtype=}
Dim TempMap As Map
TempMap.Initialize
Dim d As int
For d = 0 To (l.Size - 1)
m = l.Get (d)
tmpthreadid = m.Get ("threadid")
TempMap.Put(d,l.Get(d))
Log ("incomging " & l.Get (d))
Log ("file " & TempMap.Get(d))
RAF.Initialize2(File.DirDefaultExternal & "/" & usernametmp & "/messages/" , tmpthreadid, False,False)
'Write the whole map at once
RAF.WriteB4XObject(TempMap,0)
Next
'Then to read it back
'Read the map
Dim TempMap As M = RAF.ReadB4XObject(0)
Dim Line As String
For Each K As Int In TempMap.Keys
Line = TempMap.Get(K)
Log(Line)
Next
You are reading the same (first) object from the file each time with 'RAF.ReadB4XObject(0))'.
As Erel has suggested elsewhere on a similar topic, you would be better to save all the items to a Map, then save the Map (Unless the file is going to be very large).
Something like:
B4X:Dim TempMap As Map TempMap.Initialize Dim d As int For d = 0 To (l.Size - 1) m = l.Get (d) tmpthreadid = m.Get ("threadid") TempMap.Put(d,l.Get(d)) Log ("incomging " & l.Get (d)) Log ("file " & TempMap.Get(d)) RAF.Initialize2(File.DirDefaultExternal & "/" & usernametmp & "/messages/" , tmpthreadid, False,False) 'Write the whole map at once RAF.WriteB4XObject(TempMap,0) Next 'Then to read it back 'Read the map Dim TempMap As M = RAF.ReadB4XObject(0) Dim Line As String For Each K As Int In TempMap.Keys Line = TempMap.Get(K) Log(Line) Next
Log ("file " & RAF.ReadB4XObject(2))
java.io.IOException
B4A version: 6.50
Parsing code. Error
Error parsing program.
Error description: Unknown type: m
Are you missing a library reference?
Occurred on line: 406 (Main)
Dim tempmap As M = RAF.ReadB4XObject(0)
The position in the file is the byte position, if you want to read a particular item, you will need to store the indexes, which is why the Map solution is better. Each object is in a separate map index.
I know my code wont run as is, your code had the variable m in it, I don't know what type of variable that is, so I just quoted it.
In fact, if it's already a map, you don't need to create the temp map.
But why would I get an error then when I try to read what then turns out to be the byte at position 2?
Is that because I need to read from a position where an object starts and not in the middle of it?
I think you answered this in your next question, you can't start in the middle of an object. It would be incomplete if it did succeed.
Yes
If it's a write once, read serially set of data, then using the RAF file in the simplest way will work fine. But unless you keep an index of the start of each record, you won't be able to jump to a specific record.
If there is that much data, and it has any importance, perhaps you should be looking to store it somewhere other than an SD card anyway.