fastest way to read text file
I have a project that reads several text files and i am really shocked as to how slow the following process does it:
Code:
FileOpen(1, strFilePath, OpenMode.Input)
Do Until EOF(1)
strTextLine = LineInput(1)
:
:
Loop
FileClose(1)
is there are faster way to read text files?
Re: fastest way to read text file
Have you tried using StreamReader?
Something like;
VB Code:
Dim SR1 As StreamReader = File.OpenText("C:\ path & file )
Dim sTemp As String
Do Until SR1.Peek = -1
stemp = SR1.ReadLine
Loop
You could also read the entire file in one go with
sTemp=SR1.ReadToEnd
Re: fastest way to read text file
Quote:
Originally Posted by taxes
Have you tried using StreamReader?
You could also read the entire file in one go with
sTemp=SR1.ReadToEnd
If i use that, it will read it all in one line yeah?
How do make it so that it will put it into an arraylist line by line...so that i can refer it to
al(i) quickly?
Re: fastest way to read text file
Quote:
Originally Posted by dinosaur_uk
If i use that, it will read it all in one line yeah?
How do make it so that it will put it into an arraylist line by line...so that i can refer it to
al(i) quickly?
Using the .ReadLine and adding the line to the arraylist.
Regards
Jorge
Re: fastest way to read text file
Quote:
Originally Posted by dinosaur_uk
If i use that, it will read it all in one line yeah?
How do make it so that it will put it into an arraylist line by line...so that i can refer it to
al(i) quickly?
Not sure if you're kidding me but
VB Code:
Dim aList As New ArrayList
Dim sr1 As New StreamReader("G:\TestFile.txt")
Dim sTemp As String
Do Until sr1.Peek = -1
sTemp = sr1.ReadLine
aList.Add(sTemp)
Loop
sr1.Close()
Obviously you declare aList with the necessary scope.
Re: fastest way to read text file
Hiya
Taxes,,
Yeah I have got that ....i was thinking if there was any other way...
I am not that bad ;)
Re: fastest way to read text file
Hi,
Thought not from other posts, but one guy on the forum accuses me of being condescending at times ( apart from other things) - Hi Andy :wave:
See if this is faster
VB Code:
Dim sr1 As New StreamReader("G:\TestFile.txt")
Dim sTemp As String
Dim aList() As String
sTemp = sr1.ReadToEnd
sr1.Close()
aList = Split(sTemp, vbCrLf)
I would be interested to know.
Re: fastest way to read text file
Hi,
One other possibility, although I make no claims as to the speed, might be to use a RichTextBox with a LoadFile command (in txt mode rather than rtf) and then use the split command a la Taxes.
zaza
Re: fastest way to read text file
Hi Taxes,
The code does not work it says the 1-Dimensional array cannot be converted to a arraylist
can you please help me with this problem please please pretty please????
http://www.vbforums.com/showthread.php?t=324950
Quote:
Originally Posted by taxes
Hi,
Thought not from other posts, but one guy on the forum accuses me of being condescending at times ( apart from other things) - Hi Andy :wave:
See if this is faster
VB Code:
Dim sr1 As New StreamReader("G:\TestFile.txt")
Dim sTemp As String
Dim aList() As String
sTemp = sr1.ReadToEnd
sr1.Close()
aList = Split(sTemp, vbCrLf)
I would be interested to know.
Re: fastest way to read text file
VB Code:
Dim sr1 As New StreamReader(Application.StartupPath & "\bah.csv", FileMode.OpenOrCreate)
Dim aList As New ArrayList
Dim sTemp As String
Do Until sr1.Peek = -1
sTemp = sr1.ReadLine
aList.Add(sTemp)
Loop
MsgBox(aList.Count)
This is faster...
Re: fastest way to read text file
Re: fastest way to read text file
I am sure there must be a better way ....coz if i am handling big big csv....is there a data adaptor for it?
Re: fastest way to read text file
Quote:
Originally Posted by dinosaur_uk
Hi,
I missed this post earlier, but I am rather confused about it.
I did not mention an arraylist and the code certainly works for me.
please clarify.
Re: fastest way to read text file
yeah sorry ...i misread your post
many many apologies
Re: fastest way to read text file