Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleRead a file containing a list of numbers into an array in two ways in VB .NET.
DescriptionThis example shows how to read a file containing a list of numbers into an array in two ways in VB .NET. It reads the file one line at a time using a StreamReader object's ReadLineMethod. It then reads the file in one string using the ReadToEnd method, separates the items with the Split method, and loops through the result converting the items into Integers.
Keywordsarray, file, I/O, IO, StreamWriter, StringBuilder
CategoriesFiles and Directories
 
Note: Use the net_make_numbers program included to generate the test data file. Be sure to use the same number of items in both programs.

When the user clicks the LineInput button, the program reads the file one line at a time using a StreamReader object's ReadLine method.

 
Private Sub btnLineInput_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnLineInput.Click
    Dim start_time As Date = Now

    ' Get the number of numbers.
    Dim num As Integer = Integer.Parse(txtNumNumbers.Text)

    ' Size the array.
    Dim the_array(num - 1) As Integer

    ' Read the numbers.
    Dim stream_reader As New StreamReader(txtFile.Text)
    For i As Integer = 0 To num - 1
        the_array(i) = _
            Integer.Parse(stream_reader.ReadLine())
    Next i
    stream_reader.Close()

    Dim stop_time As Date = Now
    Dim elapsed_time As TimeSpan = _
        stop_time.Subtract(start_time)
    lblLineInput.Text = _
        elapsed_time.TotalSeconds.ToString("0.0000")
End Sub
 
When the user clicks the Split button, the program reads the entire file into a string by calling the StreamReader's ReadToEnd method. It uses the String's Replace and Split methods to separate the lines and then parses each number.
 
Private Sub btnSplit_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSplit.Click
    Dim start_time As Date = Now

    ' Get the number of numbers.
    Dim num As Integer = Integer.Parse(txtNumNumbers.Text)

    ' Size the array.
    Dim the_array(num - 1) As Integer

    ' Read the numbers.
    Dim stream_reader As New StreamReader(txtFile.Text)
    Dim all_text As String = stream_reader.ReadToEnd()
    stream_reader.Close()

    ' Separate the numbers.
    all_text.Replace(vbLf, vbCr)
    Dim lines() As String = all_text.Split(CChar(vbCr))

    ' Parse the numbers.
    For i As Integer = 0 To num - 1
        the_array(i) = Integer.Parse(lines(i))
    Next i

    Dim stop_time As Date = Now
    Dim elapsed_time As TimeSpan = _
        stop_time.Subtract(start_time)
    lblSplit.Text = _
        elapsed_time.TotalSeconds.ToString("0.0000")
End Sub
 
In my tests, the first version was about twice as fast as the second vesion.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated