Android Question How To Get Line String From .txt File

Discussion in 'Android Questions' started by Sai Main Seng Kham, Jul 19, 2019.

  1. Sai Main Seng Kham

    Sai Main Seng Kham Member Licensed User

    Hi Again:)

    I have a simple question for today
    I have the (.txt) file in my RootDir
    in the .txt file that will be have some text like this below

    I want to get the first line "123456789" as a string.
    And similarly , i want to get the second line "abcdefghijk" as another string.
    And the same way , i want to get the 3th line "zzzzzzz" as another string.

    Please Reply to my post :)
    thanks u.
     
  2. MarcoRome

    MarcoRome Expert Licensed User

    https://www.b4x.com/android/forum/threads/text-files.6690/#content

    Code:
    Sub ReadTextReader
        
    Dim TextReader1 As TextReader
        TextReader1.Initialize(
    File.OpenInput(File.DirRootExternal, "Text.txt"))
        
    Dim line As String
        line = TextReader1.ReadLine   
        
    Do While line <> Null
            
    Log(line) 'write the line to LogCat
            line = TextReader1.ReadLine
        
    Loop
        TextReader1.Close
    End Sub
     
    MarkusR likes this.
  3. DonManfred

    DonManfred Expert Licensed User

    Code:
    Dim txtlist As List = File.ReadList(File.DirAssets,"filetoread.txt")
        
    Dim str1 As String = txtlist.Get(0' Get 1st line
        Dim str2 As String = txtlist.Get(1' Get 2nd line
     
    Erel, MarkusR, mangojack and 2 others like this.
  4. Sai Main Seng Kham

    Sai Main Seng Kham Member Licensed User

    i mean the text file is not just 3 lines.

    it can be non limited line
     
  5. DonManfred

    DonManfred Expert Licensed User

    1. You can iterate over the list. I just showed an example to get 1. and 2nd line.
    2. You also can use Textreader.
     
  6. emexes

    emexes Well-Known Member Licensed User

    Putting a positive spin on it: now you have a choice of two solutions:

    1/ Marco's suggestion will give you sequential (one-at-a-time) access to an unlimited number of lines
    2/ Manfred's suggestion will give you random (direct, and in any order) access to a limited number of lines

    and can use whichever works best for this particular task.
     
    Last edited: Jul 19, 2019
  7. Sai Main Seng Kham

    Sai Main Seng Kham Member Licensed User

    thanks u everyone .
    solved
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice