B4J Question How to save settings?

trueboss323

Active Member
Licensed User
Longtime User
I have a simple question how to save app settings yet I am not able to find it on the forms. I just would like to save settings so next time app is started the settings will be loaded. It is also important that when the app is updated that I do not lose any of the settings. I also do not want the settings to be modified manually by the user (i.e text editor). Is there such a library for this ?
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
It is all about what you feel confortable with.

Just recently i used something like this (because i wanted to experiment):

1.- Create your config file using JSON
2.- Use B4x Encryption too save the config file as a random.
3.- Do reverse to load your config file.

You can actually save this file with your particular extension. (config.tru)

instead of JSON you could use a Map and use B4XSerializator to convert it to bytes those bytes convert them to string with Base64 and save file.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
There's not a magical and only solution. There are many ways to do this.
I personally use readmap and savemap and save it using appdir
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
There's not a magical and only solution. There are many ways to do this.
I personally use readmap and savemap and save it using appdir
It is all about what you feel confortable with.

Just recently i used something like this (because i wanted to experiment):

1.- Create your config file using JSON
2.- Use B4x Encryption too save the config file as a random.
3.- Do reverse to load your config file.

You can actually save this file with your particular extension. (config.tru)

instead of JSON you could use a Map and use B4XSerializator to convert it to bytes those bytes convert them to string with Base64 and save file.

Can you give an example? I just want the simplest and convenient way.
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
a simple example with a encrypted Map

NOTE: there are more than one way to do that

B4X:
'Library: jRandomAccessFile
'Main module
#Region Project Attributes 
   #AdditionalJar: bcprov-jdk15on-154
#End Region
B4X:
'Static code module
Sub Process_Globals
    Dim SettingsMap As Map
    Dim myPassword As String = "myPasswordForSettigsMap"
End Sub

Sub LoadSettings( Dir As String, FileName As String)
    If Dir="" Or FileName="" Then
        LogError( "Empty Dir or FileName")
        Return
    End If
    Dim raf As RandomAccessFile
    raf.Initialize( Dir, FileName, False)
    SettingsMap = raf.ReadEncryptedObject( myPassword, raf.CurrentPosition)
    raf.Close
End Sub

Sub SaveSettings( Dir As String, FileName As String)
    If Dir="" Or FileName="" Then
        LogError( "Empty Dir or FileName")
        Return
    End If
    Dim raf As RandomAccessFile
    raf.Initialize( Dir, FileName, False)
    raf.WriteEncryptedObject( SettingsMap, myPassword, raf.CurrentPosition)
    raf.Close
End Sub

Sub Test
    SettingsMap.Initialize
   
    'Set and Save
    SettingsMap.Put( "B4J", "is good")
    Log( SettingsMap.GetDefault( "B4J", "is better"))
    SaveSettings( File.DirApp, "settings.conf")

    'Load and Get
    LoadSettings( File.DirApp, "settings.conf")
    Log( SettingsMap.GetDefault( "B4J", "is better"))

    'Set and Save
    SettingsMap.Put( "B4J", "is the Best")
    Log( SettingsMap.GetDefault( "B4J", "is better"))
    SaveSettings( File.DirApp, "settings.conf")
End Sub
 
Upvote 0
Top