VB 2005 question regarding reading text files

akalmand

Member
Joined
Jul 12, 2007
Messages
8
Programming Experience
Beginner
Hi there,

I am writing a code to read some data from the text files. The number of text files is not fixed and could be more that 15. the length of each file is large... close to 100,000 on an average. some of them are extra large. The data that I have to read will always be at the bottom and will be in the last 5 -20 line in the files depending upon their size. small files will have 5 line and large files will have 20 lines to read.

Can you people suggest me an easy way to do it. If I read all the files from top and look for a particular word to start storing data..with 15 file it will take a lot of time.

is there are way to read from bottom... or count the number of line and start reading from no. of line - 20....

or any other easy ways??

please let me know..

thanks... please keep in mind that i am a begineer...
 
I don't think there is an "easy" way to do what you want. The problem is that the "start" point is not clearly defined. If you had some mathematical way to determine (based on file size) where to start, then a StreamReader/FileStream object might be able to do what you are asking.
 
100kb is quite large textfile, but it isn't much to put temporary into memory when processing, here is an example:
VB.NET:
    Sub processFile(ByVal filename As String)
        Dim text As String = My.Computer.FileSystem.ReadAllText(filename)
        Dim lines() As String = System.Text.RegularExpressions.Regex.Split(text, vbNewLine)
        For i As Integer = lines.Length - 20 To lines.Length - 1
            lines(i) = "replaced line " & i + 1
        Next
        text = String.Join(vbNewLine, lines)
        My.Computer.FileSystem.WriteAllText(filename, text, False)
    End Sub
Doing this with a 200kb textfile with more than 5000 textlines took only 26 milliseconds to process.

If you only wanted to read and removed the join&save lines the time was reduced to 10 ms for the same file.
 
Back
Top