B4J Question RandomAccessFiles how to access randomly?

SimonElck

Member
Licensed User
I probably missed something,
but how can I access a record N in a random access file?
And how can I replace record N with new values?
In the examples I can only see sequentially reading through the file with RAF.CurrentPosition

In good old basic there were instructions to SEEK a record, given a fixed record length
with instructions like FIELD.
Of course I could create a user TYPE myType of fixed length and then do:

B4X:
RAF.ReadObject(recnum * myType.length)

Is this what you suggest? Or, as said, do I miss something important?
I presume that using COMPRESS=true would make it impossible to use my solution as the length of the record would not be preserved?
 

stevel05

Expert
Licensed User
Longtime User
Random access file implementation isn't intended to be used in that way, It gives access to the contents of a file at a low level so you can write and obtain data if you know the position of it. It COULD be used that way if you managed it all. SQLite is a much better solution for accessing structured data have a look at DBUtils for a start. Or if it is a very simple data structure, you could use the KeyValueStore Class (which implements SQLite in the background).
 
Upvote 0

SimonElck

Member
Licensed User
Thanks, Stevel05,
I haven't written classes in a very long time, but I think I will give it a shot and try and write a FIELD class.
It's the only thing missing in using RandomAccessFiles in the way I suggested (ie Random Access).
In my opinion it would provide a much lighter solution than SQLite in all the cases where one didn't need the extra overhead of said library.
But it's OK if you disagree
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
I understand where you're coming from Simon, I remember writing file access that way too. These days, when storage space, memory and speed is not much of an issue, I prefer to use tools crafted for the job in hand. While it would certainly work, I would prefer the speed and accessibility of a database API, especially if you want to deal with multiple data structures.

Also bear in mind that you will undoubtedly want to store data for your programs operation as well, window positions, global variable values etc. I would always go with the simplest solution.

If you're doing it for the hell of it and because you can, that's great. Have fun.
 
Upvote 0

SimonElck

Member
Licensed User
But there is no way of knowing where a record starts and no way to replace records when there is no fixed record length.
That's why there was a FIELD and an LSET instruction in oldies like QBasic and even longer ago GW_Basic. (Just for us oap's)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…