[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. :afrog:
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:
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.
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:
Dim aList() As String = System.IO.File.ReadAllLines("C:\Test.txt")
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.