Results 1 to 4 of 4

Thread: [RESOLVED] [2005] How to read a block of text line by line?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Resolved [RESOLVED] [2005] How to read a block of text line by line?

    Hi,

    I mainly process text files and if I need to run through the file twice, I'll open and read through the file twice, line by line, picking out the info I need.

    I know this is inefficient. I was thinking of reading a text file line by line and putting each line into a List of String and then I could reprocess using that.

    Or could a text file be read as a block, e.g. IO.ReadAllText(), and somehow processed line by line?

    What do you suggest? I mentioned twice above, but actually it might be up to a dozen times.

    Thx for your tips.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] How to read a block of text line by line?

    You could simply split the contents by each new line if that's what always happens in your text file, or similarly the symbol that marks the end of each line.
    Load each into a string array:

    VB Code:
    1. Dim myStr() As String = My.Computer.FileSystem.ReadAllText("D:\My Documents\MyExample.txt").Split(Environment.NewLine)

    That will place each line of your text file into an array.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3
    New Member
    Join Date
    May 2004
    Location
    Norway
    Posts
    2

    Re: [2005] How to read a block of text line by line?

    I would say the following is a tad more efficient, at the very least smaller, than the above:
    VB Code:
    1. Dim aList() As String = System.IO.File.ReadAllLines("C:\Test.txt")

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    252

    Re: [2005] How to read a block of text line by line?

    Waha! So that's what RealAllLines is for. Doh.

    Thx both for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width