Android Question Delete certain line in text file

qey

Member
How to delete line in text file ? I have 5 lines that I saved in text file, but i want to delete the last line and replace with a new value. Any idea ?
 

Chris2

Active Member
Licensed User
Longtime User
B4X:
Dim l As List = File.ReadList("filePath", "filename")    'put each line of the text file into a list
l.RemoveAt(l.Size-1)                                    'remove the last line

l.Add("new line text here")                                   'add your new line
File.WriteList("savePath", "filename", l)                'save the list to a new file (if necessary)
 
Upvote 0

MikeH

Well-Known Member
Licensed User
Longtime User
Thats the way I first thought but, as is the beauty of coding, there are other ways:

B4X:
dim str as string = File.ReadString (Dir, FileName)
str = str.replace(str.substring(str.LastIndexOf(crlf)), crlf & "New value")
 
Last edited:
Upvote 0
Top