Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read text file through stream reader

I have a text file name list.txt which contains data like following;

AC-No.                 Name                Time        State    New State Exception

    100            ZULFIQUAR 09/04/2012 01:53 PM         C/In                Invalid
    100            ZULFIQUAR 10/04/2012 01:39 PM         C/In                Invalid
    100            ZULFIQUAR 11/04/2012 01:38 PM         C/In                Invalid
   1002                SAQIB 09/04/2012 10:42 PM         C/In        C/Out        OK
   1002                SAQIB 10/04/2012 08:01 AM         C/In                     OK
   1002                SAQIB 10/04/2012 10:28 PM         C/In        C/Out        OK
   1002                SAQIB 11/04/2012 09:25 AM         C/In                     OK
   1002                SAQIB 11/04/2012 10:40 PM         C/In        C/Out        OK
   1002                SAQIB 12/04/2012 07:15 AM         C/In                     OK
   1002                SAQIB 12/04/2012 11:12 PM         C/In        C/Out        OK
   1002                SAQIB 13/04/2012 07:23 AM         C/In                     OK
   1002                SAQIB 13/04/2012 10:53 PM OverTime Out                Invalid
   1002                SAQIB 14/04/2012 06:58 AM OverTime Out                Invalid
   1002                SAQIB 15/04/2012 10:50 PM         C/In                Invalid
   1002                SAQIB 16/04/2012 07:09 AM         C/In                     OK
   1002                SAQIB 17/04/2012 10:36 PM         C/In        C/Out        OK
   1002                SAQIB 18/04/2012 07:21 AM         C/In                     OK
   1002                SAQIB 18/04/2012 10:46 PM         C/In        C/Out        OK
   1002                SAQIB 19/04/2012 06:32 AM         C/In                     OK
   1002                SAQIB 19/04/2012 10:47 PM         C/In        C/Out        OK

now i have to pick three columns(AC-No,Name.Time) with entire row and give datagridview its data source. for now i am using following code but no luck.

Dim tbl As New DataTable("mytable")
        tbl.Columns.Add("col1", GetType(String))
        'tbl.Columns.Add("col2", GetType(String))
        'tbl.Columns.Add("col3", GetType(Integer))
        Dim sFilename As String = TextBox1.Text
        Dim myStream As System.IO.StreamReader = New System.IO.StreamReader(sFilename)
        Dim line As String
        Dim aRow As DataRow
        Do
            line = myStream.ReadLine()
            If line Is Nothing Then
                Exit Do
            End If
            Dim sAry As String() = Split(line, "    ")
            aRow = tbl.NewRow
            aRow(0) = sAry(0)
            'aRow(1) = sAry(1)
            '  aRow(2) = sAry(2)
            tbl.Rows.Add(aRow)
        Loop
        myStream.Close()
        DataGridView1.DataSource = tbl

please help me.

like image 522
Asif khan Avatar asked Jun 23 '26 08:06

Asif khan


1 Answers

Dim delimiter As String = ","
Dim fileName As String = "c:\file.txt"

Dim sr As New StreamReader(fileName)

Try
    While sr.Peek() >= 0
        Dim r As String = sr.ReadLine()
        Dim items As String() = r.Split(delimiter.ToCharArray())
    End While
Finally
    sr.Close()
End Try
like image 164
Rejeesh Avatar answered Jun 28 '26 12:06

Rejeesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!