Type & RandomAccessFile

FabioG

Active Member
Licensed User
Longtime User
Hello to all,
I have a problem with an application in which to save the settings and use Type RandomAccessFile.

I have the following code

B4X:
Type WiFiCfg(SSID As String,WiFiKey As String,EncryptionKey As String,Channel As Int)
Dim WiFiCfg1 As WiFiCfg

B4X:
Sub SettingsLoad

   If File.Exists(File.DirInternal, "Setings.dat") Then   
      Rf.Initialize(File.DirInternal,"Setings.dat",True)
      WiFiCfg1 = Rf.ReadObject(Rf.CurrentPosition)
      Rf.Close      
   End If
   
End Sub



Sub SettingsWrite
   WiFiCfg1.SSID = EditText_SSID.Text
   WiFiCfg1.WiFiKey = EditText_WiFiKey.Text
   WiFiCfg1.EncryptionKey = Spinner_EncryptionKey.SelectedItem
   WiFiCfg1.Channel = Spinner_Channel.SelectedItem
   Rf.Initialize(File.DirInternal,"Setings.dat",False)
   Rf.WriteObject(WiFiCfg1,False,Rf.CurrentPosition)
   Rf.Close
End Sub

I recently changed this

B4X:
Type WiFiCfg(SSID As String,WiFiKey As String,EncryptionKey As String,Channel As Int)
adding a new field

B4X:
Type WiFiCfg(SSID As String,WiFiKey As String,EncryptionKey As String,Channel As Int,Broadcast as Boolean)

but now when I go to load the object reflected this error "LastException java.io.EOFException"

in WiFiCfg1 = Rf.ReadObject(Rf.CurrentPosition)

How can I fix?

thank you very much
 

stevel05

Expert
Licensed User
Longtime User
You could try creating a temporary type that recreates the old version, reading the existing file into that, then copying the data into the new type with the additional field then saving that one. Although I'd use a different filename to be safe! and/or make sure you have a backup.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I've had this same problem (many times!)
Now I dont use RAF.WriteObject anymore, I write it out manually using TextWriter.
But everytime I change the type I need to add some code to be able to port it to the new type.
I do exactly as stevel05 says.

Somthing like this on the first time the app is run:

B4X:
Sub ImportOldData
      ReadOldFile
      SaveNewFile
End Sub
 
Upvote 0

FabioG

Active Member
Licensed User
Longtime User
ok guys is clear, but there are other methods that are not part of this text file to save your settings?

Thank you very much
 
Upvote 0

FabioG

Active Member
Licensed User
Longtime User
KeyValueStore is a good solution for storing such types. However it also uses internally RAF.ReadObject. ReadObject / WriteObject methods are good as long as you do not change the types.

Ok Erel!

tnks :)
 
Upvote 0
Top