Android Question Update a line within a TXT File

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
I have read how to create a text file, how to insert a line and how to search for some stored data.

I have created a comma delimited text file as you can see in the image.

1665234520053.png

I look for the information with the following code in B4A

B4X:
Public Sub Func_SearchUser(user As String) As Boolean
    Dim ret As Boolean=False
    Dim List1 As List
    List1 = File.ReadList(File.DirInternal, namearch)
    For i = 0 To List1.Size - 1
        Dim array() As String = Regex.Split(",",List1.Get(i))
        Dim userbd=array(0)
        If userbd=user Then
            ret=True
            Exit
        Else
            ret=False
        End If
    Next
    Return ret
End Sub
Need to be able to update a player's credits. One solution that I think would work would be to read the entire file using arrays and dump that to a new text file and then rename that new file.

Is there any other easier way to update certain data of the line that is in the text file?

Thank you, as always, for your valuable collaboration.
 

Mahares

Expert
Licensed User
Longtime User
Is there any other easier way to update certain data of the line that is in the text file?
Bladimir, you are good at SQL . Why don't use a nice simple SQLite database to store the data from the text file and easily insert, update, delete, read or whatever. If you are intent to use a text file, please ignore this post.
 
Upvote 0

Xandoca

Active Member
Licensed User
Longtime User
I have read how to create a text file, how to insert a line and how to search for some stored data.

I have created a comma delimited text file as you can see in the image.

View attachment 134539
I look for the information with the following code in B4A

B4X:
Public Sub Func_SearchUser(user As String) As Boolean
    Dim ret As Boolean=False
    Dim List1 As List
    List1 = File.ReadList(File.DirInternal, namearch)
    For i = 0 To List1.Size - 1
        Dim array() As String = Regex.Split(",",List1.Get(i))
        Dim userbd=array(0)
        If userbd=user Then
            ret=True
            Exit
        Else
            ret=False
        End If
    Next
    Return ret
End Sub
Need to be able to update a player's credits. One solution that I think would work would be to read the entire file using arrays and dump that to a new text file and then rename that new file.

Is there any other easier way to update certain data of the line that is in the text file?

Thank you, as always, for your valuable collaboration.
I think that there's no other option when working with text files. Here you can see some implementations...
 
Upvote 0

Bladimir Silva Toro

Active Member
Licensed User
Longtime User
Bladimir, you are good at SQL . Why don't use a nice simple SQLite database to store the data from the text file and easily insert, update, delete, read or whatever. If you are intent to use a text file, please ignore this post.
Hello @Mahares

If you are absolutely right, it would be much easier to do it with a database with SQL queries, but I am teaching my students to manage text files in B4A

I hope you can help me with a good solution in B4A.

Thank you very much
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
hope you can help me with a good solution in B4A.
Have you looked at CSVParser. Here is a full example. If not suitable, keep asking for a better solution:
You need the CSVParser class module. When you search for CSVParser. it is the first post
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private header() As String
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
'    File.Delete(xui.DefaultFolder, "1.csv")
    If Not(File.Exists(xui.DefaultFolder, "1.csv")) Then
        File.Copy(File.DirAssets, "1.csv", xui.DefaultFolder,"1.csv")
    End If
    header= Array As String("Player", "Pass", "Credit")
    
    Modify("bladimir", "9999")
End Sub

Sub Modify(Player  As String, Credit As String)
    Dim parser As CSVParser
    parser.Initialize
    Dim table As List = parser.Parse(File.ReadString(xui.DefaultFolder, "1.csv"), ",", True)
    For Each row() As String In table
        If row(0).tolowercase.Contains(Player ) Then
            Log("found")
            row(2) = Credit
            Exit
        End If
    Next
    table.InsertAt(0, header)
    Dim s As String = parser.GenerateString(table, ",")
    File.WriteString(xui.DefaultFolder, "1.csv", s)
    
    Dim table As List = parser.Parse(File.ReadString(xui.DefaultFolder, "1.csv"), ",", False)
    For Each sarray() As String In table
        Log($"${sarray(0)}  ${sarray(1)}  ${sarray(2)}"$)
    Next    
End Sub
 
Upvote 0

PaulMeuris

Active Member
Licensed User
Here is another possible solution for the CSV text file problem.
The implementation you find via this link: CSV_textfile uses a CustomListView (CLV) to visualize the CSV text file information.
You can add, change or delete information using a dialog and save and load the content of the CLV.
The find button can be used to search for information in the CLV.
 
Upvote 0
Top