Results 1 to 15 of 15

Thread: fastest way to read text file

  1. #1

    Thread Starter
    Hyperactive Member stingrae's Avatar
    Join Date
    Apr 2002
    Location
    Sydney
    Posts
    401

    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?
    "The passion lives to keep your faith, though all are different, all are great" ... Michael Hutchence 1960-1997.

    Windows & Web Developer
    Specialising in Visual Basic .Net & Client Server Programming & Client/Customer Relations Databases
    Sutherland Shire, Sydney Australia
    www.stingrae.com.au
    Developer of Arnold - Gym & Martial Arts Database Management System
    www.gymdatabase.com.au

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: fastest way to read text file

    Have you tried using StreamReader?

    Something like;

    VB Code:
    1. Dim SR1 As StreamReader = File.OpenText("C:\  path & file )
    2. Dim sTemp As String
    3. Do Until SR1.Peek = -1
    4.   stemp = SR1.ReadLine
    5. Loop

    You could also read the entire file in one go with

    sTemp=SR1.ReadToEnd
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  3. #3
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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?

  4. #4
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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
    "The dark side clouds everything. Impossible to see the future is."

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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:
    1. Dim aList As New ArrayList
    2.  
    3.  
    4.         Dim sr1 As New StreamReader("G:\TestFile.txt")
    5.         Dim sTemp As String
    6.        
    7.         Do Until sr1.Peek = -1
    8.             sTemp = sr1.ReadLine
    9.             aList.Add(sTemp)
    10.         Loop
    11.         sr1.Close()

    Obviously you declare aList with the necessary scope.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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

    See if this is faster

    VB Code:
    1. Dim sr1 As New StreamReader("G:\TestFile.txt")
    2.         Dim sTemp As String
    3.         Dim aList() As String
    4.         sTemp = sr1.ReadToEnd
    5.         sr1.Close()
    6.         aList = Split(sTemp, vbCrLf)

    I would be interested to know.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    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

  9. #9
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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

    See if this is faster

    VB Code:
    1. Dim sr1 As New StreamReader("G:\TestFile.txt")
    2.         Dim sTemp As String
    3.         Dim aList() As String
    4.         sTemp = sr1.ReadToEnd
    5.         sr1.Close()
    6.         aList = Split(sTemp, vbCrLf)

    I would be interested to know.

  10. #10
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: fastest way to read text file

    VB Code:
    1. Dim sr1 As New StreamReader(Application.StartupPath & "\bah.csv", FileMode.OpenOrCreate)
    2.         Dim aList As New ArrayList
    3.         Dim sTemp As String
    4.         Do Until sr1.Peek = -1
    5.             sTemp = sr1.ReadLine
    6.             aList.Add(sTemp)
    7.         Loop
    8.         MsgBox(aList.Count)

    This is faster...

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: fastest way to read text file

    Hi,

    Thanks
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    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?

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: fastest way to read text file

    Quote Originally Posted by dinosaur_uk
    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
    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.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: fastest way to read text file

    yeah sorry ...i misread your post

    many many apologies

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: fastest way to read text file

    No problem
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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