【问题标题】:Read text file through stream reader通过流阅读器读取文本文件
【发布时间】:2012-04-30 11:15:39
【问题描述】:

我有一个文本文件名 list.txt,其中包含如下数据;

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

现在我必须选择整行的三列(AC-No,Name.Time),并为 datagridview 提供其数据源。 现在我正在使用以下代码,但没有运气。

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

请帮帮我。

【问题讨论】:

  • 有什么问题,什么不起作用?

标签: vb.net filestream


【解决方案1】:
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

【讨论】:

  • 谢谢拉吉什。但仍然没有成功。实际上我希望有没有空格的数据,因为我必须在 sql 表 03 中插入​​整列,列将插入到 sql 表的 03 列中,其中包含一堆行。请查看
【解决方案2】:

假设 1) 您显示了一个具有固定列宽的文件,2) 您需要将时间作为 DateTime 而不是字符串,以及 3) AC-No.真的是一个字符串而不是一个整数:

Imports System.Globalization
Sub GetData()
    Dim tbl As New DataTable("mytable")
    tbl.Columns.Add("col1", GetType(String))
    tbl.Columns.Add("col2", GetType(String))
    tbl.Columns.Add("col3", GetType(DateTime))

    Dim sFilename As String = "C:\temp\testdata.txt"

    Using myStream As System.IO.StreamReader = New System.IO.StreamReader(sFilename)
        Dim ac As String
        Dim username As String
        Dim clocktime As DateTime
        Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("en-GB")

        Dim line As String
        Dim aRow As DataRow
        While Not myStream.EndOfStream
            line = myStream.ReadLine().TrimEnd
            ' check line is long enough to be valid
            If line.Length >= 48 Then
                ' take the first 8 characters as AC-No.
                ac = line.Substring(0, 8).Trim
                ' a valid line does not start with "AC-No"
                If Not ac.StartsWith("AC-No") Then
                    ' extract the name and remove any extra spaces
                    username = line.Substring(9, 20).Trim
                    ' extract the time and convert it to a DateTime
                    clocktime = DateTime.Parse(line.Substring(29, 19), culture, DateTimeStyles.AllowWhiteSpaces)
                    aRow = tbl.NewRow
                    aRow(0) = ac
                    aRow(1) = username
                    aRow(2) = clocktime
                    tbl.Rows.Add(aRow)
                End If
            End If

        End While

    End Using

    DataGridView1.DataSource = tbl
    ' example of formatting the third column
    DataGridView1.Columns(2).DefaultCellStyle.Format = "dd-MMM-yyyy HH:mm"

End Sub

(测试为在 VB2010 中使用您的数据,如图所示。)

【讨论】:

  • @Asifkhan 不客气。如果它回答了您的问题,请将其标记为答案。
【解决方案3】:

只是一个建议,尝试 mystream.readtoend() ,然后在每个 vbnewline 处切片,之后每行都有一个单独的字符串,您可以在其中调试和播放,直到得到正确的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多