如何在vb.net中同时逐行读取两个文本文件

时间:2015-01-20 15:59:56

标签: vb.net streamreader

HELP!

我想同时读取两个文本文件,我可以逐行读取文本文件,但是当我添加1个文本文件同时读取时它只读取第一行而已。

例如:

在name.txt中有

enter image description here

并且在age.txt中有

enter image description here

我想同时阅读它们,以便我可以将它们保存到数据库中,并附上相应的名称和年龄。

这是我的代码:

Public Sub ReadName()
    On Error Resume Next
    Dim FileReader As System.IO.StreamReader
    FileReader = My.Computer.FileSystem.OpenTextFileReader(des & "\name.txt")
    Dim stringReader As String
    stringReader = FileReader.ReadLine
    txtName.Text = stringReader
    FileReader.Close()
End Sub

和年龄相同的代码

Public Sub ReadAge()
    On Error Resume Next
    Dim FileReader As System.IO.StreamReader
    FileReader = My.Computer.FileSystem.OpenTextFileReader(des & "\age.txt")
    Dim stringReader As String
    stringReader = FileReader.ReadLine
    txtAge.Text = stringReader
    FileReader.Close()
End Sub

我已经尝试了γηράσκωδ'αείπολλάδιδασκόμε提供的代码,但是文本框中显示的唯一值是最后一个..我只是想在每次读取每一行时在文本框上显示它..我把它放在计时器中,当表单加载时,计时器将被启用..

Dim FileReaderName, FileReaderAge As System.IO.StreamReader
    FileReaderName = My.Computer.FileSystem.OpenTextFileReader("C:\Users\toshiba\Desktop\from sky\name.txt")
    FileReaderAge = My.Computer.FileSystem.OpenTextFileReader("C:\Users\toshiba\Desktop\from sky\age.txt")

    Dim nameReader, ageReader As String

    Do While FileReaderName.Peek() >= 0 And FileReaderAge.Peek() >= 0
        nameReader = FileReaderName.ReadLine
        ageReader = FileReaderAge.ReadLine

        TextBox1.Text = nameReader
        TextBox2.Text = ageReader
    Loop

    FileReaderName.Close()
    FileReaderAge.Close()

2 个答案:

答案 0 :(得分:1)

如果文本文件不是太大,您可以通过一次只读取它们来简化任务。

使用ReadAllLines将所有行提取到数组中:

Dim Names() As String = IO.File.ReadAllLines(IO.Path.Combine(des, "name.txt"))
Dim Ages() As String = IO.File.ReadAllLines(IO.Path.Combine(des, "age.txt"))

然后迭代:

For i = 0 To Math.Min(Names.Count, Ages.Count) - 1
   'Add Names(i) and Ages(i) to database
Next

答案 1 :(得分:0)

打开两个文件并循环执行:

Dim nameReader, ageReader As String

'loop through all lines
nameReader = FileReaderName.ReadLine
ageReader = FileReaderAge.ReadLine

修改

Dim FileReaderName, FileReaderAge As System.IO.StreamReader
FileReaderName = My.Computer.FileSystem.OpenTextFileReader(des & "\name.txt")
FileReaderAge = My.Computer.FileSystem.OpenTextFileReader(des & "\age.txt")

Dim nameReader, ageReader As String

Do While FileReaderName.Peek() >= 0 And FileReaderAge.Peek() >= 0
    nameReader = FileReaderName.ReadLine
    ageReader = FileReaderAge.ReadLine
    'Do what you want with nameReader, ageReader 

Loop

FileReaderName.Close()
FileReaderAge.Close()

<强> EDIT2

backgroundworker添加form。在form load活动中:

BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()

添加以下事件:

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim FileReaderName, FileReaderAge As System.IO.StreamReader
    FileReaderName = My.Computer.FileSystem.OpenTextFileReader(des & "\name.txt")
    FileReaderAge = My.Computer.FileSystem.OpenTextFileReader(des & "\age.txt")

    Dim nameReader, ageReader As String

    Do While FileReaderName.Peek() >= 0 And FileReaderAge.Peek() >= 0
        System.Threading.Thread.Sleep(1000) 'Change the value if you want faster or slower 

        nameReader = FileReaderName.ReadLine
        ageReader = FileReaderAge.ReadLine

        BackgroundWorker1.ReportProgress(100, nameReader)
        BackgroundWorker1.ReportProgress(50, ageReader)
    Loop

    FileReaderName.Close()
    FileReaderAge.Close()
End Sub

Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    If e.ProgressPercentage = 100 Then
        TextBox1.Text = e.UserState.ToString
    Else
        TextBox2.Text = e.UserState.ToString
    End If
End Sub