StreamReader Class read two column data

godspeed

New member
Joined
Jun 14, 2006
Messages
1
Programming Experience
Beginner
Hello,
I am wanting to create a StreamReader for reading a file, the file has two colum data seperated by commas. Being this is the case how can store the data into separate arrays?

data example:
fifo,ldap

Thanks!
 
Example that puts the two columns into two StringCollection instances col1 and col2. For the data sample you gave it need not be more complicated than this, no special parsing of quoted fields that may contain the delimiter inside and no error-checking if all lines contains two fields. Else you should have a look at the parsing example of this thread http://www.vbdotnetforums.com/showthread.php?t=11105
VB.NET:
Sub csv3()
 Dim filename As String = "sample2.csv"
 Dim col1 As New Collections.Specialized.StringCollection
 Dim col2 As New Collections.Specialized.StringCollection
 Dim delimiter As String = ","
 Dim fields() As String
 Dim fs As New IO.FileStream(filename, IO.FileMode.Open, IO.FileAccess.Read)
 Dim sr As New IO.StreamReader(fs)
 While sr.Peek <> -1
  fields = sr.ReadLine.Split(delimiter)
  col1.Add(fields(0))
  col2.Add(fields(1))
 End While
 sr.Close()
 fs.Close()
End Sub
 
Back
Top