B4J Question [SOLVED] How to delete a line from a text file?

christianjeannot

Member
Licensed User
Hello community,

I have searched the forum how I can delete a line from a text file. In the results I found no usefull information. May be I searched wrong.

Is it possible with B4J to delete a line from a text file or do I have to rewrite the file except the line I want to delete?

Best regards

--Christian
 

Sandman

Expert
Licensed User
Longtime User
That question sounds so simple, but there are many follow-up questions needed to better answer it...
  • Roughly what size is the file? And roughly how many lines?
  • What OS is the B4J app meant to run on?
  • How do you identify what line to delete?
  • Do you wish to delete in the file itself or create a new file without the deleted line?
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
example of code
Yep, this is a perfect solution for a subset of the possible cases: When the file isn't very large and you know the line number you want to delete.

However, if the file is very large and you don't know the line number, you will need another method. Hence my questions, to better understand the context.
 
Upvote 0

christianjeannot

Member
Licensed User
Hello Community,

many thanks for your information.

@Sandman
It is a smal file with not many lines. I do not expect more than 100 lines.
The App is a multi-platform App. B4J ist the start. B4A and B4i will follow.
The line is like a CSV file with ";" as delimiter. I know exactly one field to identify the line to delete.

@Quandalle
Thank you for the example code. I will test it.

Best regards

--Christian
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
It is a smal file with not many lines. I do not expect more than 100 lines.
The App is a multi-platform App. B4J ist the start. B4A and B4i will follow.
The line is like a CSV file with ";" as delimiter. I know exactly one field to identify the line to delete.
Great, in that case the code by @Quandalle will work for you. You will need to adjust line #3 obviously, to find the correct line to delete.
 
Upvote 0

Quandalle

Member
Licensed User
for a very large file that cannot be loaded into memory using a ReadList approach, we can use a line-by-line reading.

Sample code :
B4X:
...
    Dim streamInput = File.OpenInput(Path, filename)
    Dim streamOutput = File.OpenOutput(Path,filename,False)
    Dim tr As TextReader
    Dim tw As TextWriter
    tr.Initialize(streamInput) ' or intalize2 if encoding is needed, for example r.Initialize2(streamInput, "ISO-8859-1")
    tw.initialize(streamOutput) ' or initailize2
    
    Dim strLine As String
    
    strLine = tr.ReadLine
    Do While (strLine <> Null)
        If not (linetodelete) Then
            tw.WriteLine(strLine)
        End If
        strLine = tr.ReadLine
    Loop
    
    tw.Close
    tr.Close
...
 
Upvote 0

christianjeannot

Member
Licensed User
@Quandalle

In my test I have use a text file with e.g.

B4X:
ABC;13132
DEF;
JJJ;13132
KKK;13132
TTT;13132;13132
EEE;13132
JJJ;13132;13132
ZZZ;13132

When I use a list and search for e.g. DEF I got -1. When I search for DEF; I got 1.

Is it possible to search in a list when I only know a part from the line?

Best regards

--Christian
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Iterate trough the list, get a Item (String). You can use any string method on this string to find something.

 
Upvote 0

Quandalle

Member
Licensed User
@Quandalle

In my test I have use a text file with e.g.

B4X:
ABC;13132
DEF;
JJJ;13132
KKK;13132
TTT;13132;13132
EEE;13132
JJJ;13132;13132
ZZZ;13132

When I use a list and search for e.g. DEF I got -1. When I search for DEF; I got 1.

Is it possible to search in a list when I only know a part from the line?

Best regards

--Christian

Assuming that the data are in the file test.txt, and that the searched part is at the beginning of the line, we can use the following code :
B4X:
    ...
    Dim m As List =  File.ReadList(File.DirApp,"test.txt")

    For i=0 To m.Size-1
        Dim s As String = m.Get(i)
        If s.StartsWith("DEF;") Then
            m.RemoveAt(i)
            Exit
        End If
    Next
    File.WriteList(File.DirApp,"test.txt",m)
    ...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Assuming that the data are in the file test.txt, and that the searched part is at the beginning of the line, we can use the following code
it will not work.
You need to go from m.Size-1 to 0 step -1
B4X:
    For i=m.Size-1 to 0 step -1
        Dim s As String = m.Get(i)
        If s.StartsWith("DEF;") Then
            m.RemoveAt(i)
            Exit
        End If
    Next
 
Upvote 0

Quandalle

Member
Licensed User
it will not work.
You need to go from m.Size-1 to 0 step -1
B4X:
    For i=m.Size-1 to 0 step -1
        Dim s As String = m.Get(i)
        If s.StartsWith("DEF;") Then
            m.RemoveAt(i)
            Exit
        End If
    Next

if there is only one line to destroy you can go from 0 to size-1 or in reverse order from size-1 to 0 it's the same. The exit instruction stops the loop anyway.
If there are several lines to destroy, you have to go from the last to the first of the list (Size-1 to 0) and above all remove the exit instruction from the code.
 
Upvote 0

christianjeannot

Member
Licensed User
Hello Comunity,

@Quandalle and @DonManfred
Thank you for the code snippet.

I have made some tests.

As I have several lines I had to start from the end. When I start from begin I run into an error.

This is my final code with a successfull result.

B4X:
Dim m As List
    Dim ToDelete As String
    ToDelete = "DEF"
    If File.Exists(DataFolder,"SaveWriteFile2.txt") = True Then
        Log("File exist")
        m=File.ReadList(DataFolder, "SaveWriteFile2.txt")
        For i=m.Size-1 To 0 Step -1
            Dim s As String             
            s = m.Get(i)
            If s.StartsWith(ToDelete) Then
                m.RemoveAt(i)
            End If
        Next
        File.WriteList(DataFolder,"SaveWriteFile2.txt",m)
    End If

Many thanks for the support.

Best regards

--Christian
 
Upvote 0

Quandalle

Member
Licensed User
it is probably better to include the separator (";") in the StartWith, this avoids deleting a line that (using DEF as an example) would start with DEF but would have extra characters (i.e. DEFI, ...)

B4X:
Dim m As List
    Dim ToDelete As String
    ToDelete = "DEF" +";" '===> add separtor
    If File.Exists(DataFolder,"SaveWriteFile2.txt") = True Then
        Log("File exist")
        m=File.ReadList(DataFolder, "SaveWriteFile2.txt")
        For i=m.Size-1 To 0 Step -1
            Dim s As String             
            s = m.Get(i)
            If s.StartsWith(ToDelete) Then
                m.RemoveAt(i)
            End If
        Next
        File.WriteList(DataFolder,"SaveWriteFile2.txt",m)
    End If
 
Upvote 0
Top