Android Question Read a specfic line of a text file

ginsh

Member
Licensed User
Longtime User
Hi All,

Please let me know how to read a specific line of a text file by giving a line number.
 

DonManfred

Expert
Licensed User
Longtime User
Read the lines into a list and return the value index-1
Depends on the fact the list is not too big and it reads the hole file into a list.

Alternatively you can use Stringreader to read lines until you reach the wanted index. Probably much slower then using a List.

B4X:
Log(GetLine(1))
Log(GetLine(10))

Sub GetLine(idx As Int) As String
    Dim text As List = File.ReadList(File.DirAssets,"test.txt")
    If text.IsInitialized And text.Size>=idx-1 Then
        Return text.Get(idx-1)
    End If
    Return ""
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…