B4J Question Best way to save Textarea data to a file

rfresh

Well-Known Member
Licensed User
Longtime User
I've been searching in the forum and outside of the forum, and I can't find an example of how to save Textarea data to a text file?

Can I write the entire Textarea in one line or do I need to use a for next loop?

Thank you for any help...
 

mangojack

Expert
Licensed User
Longtime User
I've been searching in the forum and outside of the forum, and I can't find an example of how to save Textarea data to a text file?

This is an Android tutorial , but the same principle applies ...

Can I write the entire Textarea in one line
Yes ... no need to Loop.

You could use the following ...
B4X:
File.WriteString(File.DirData("MyApp"), "Text.txt", TextArea1.Text)   'write text to file

TextArea1.Text = File.ReadString(File.DirData("MyApp"), "Text.txt")   'read text from file


You could also use KeyValueStore (Internal lib) ... It is very simple to use and can store much more in single database (App settings, maps, lists etc)
B4X:
Private KVS As KeyValueStore  'Process_Global

KVS.Initialize(File.DirData("MyApp"), "datastore.db")  'App Start


KVS.Put("TextArea1 Contents", TextArea1.Text)        'write data

'If KVS.ContainsKey("TextArea1 Contents") Then
    TextArea1.Text = KVS.Get("TextArea1 Contents")   'get the data ...  (your choice to check that key exists first.)
'End If
 
Last edited:
Upvote 0
Top