0

HELP!

i want to read two text file at the same time, i could read a text file line by line but when i added 1 text file to read at the same time it only read the first line and nothing more..

for example:

in name.txt it has

enter image description here

and in age.txt it has

enter image description here

i want to read them simultaneously so that i could save them into the database with their corresponding name and age..

here is my code:

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

and same code for the age

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

i've tried the code that γηράσκω δ' αεί πολλά διδασκόμε provided but the only value that would display in the textbox is the last one.. i just want to display it on the textbox everytime it reads each line.. i put it in the timer and when the form loads the timer will be enabled..

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()
tris
  • 39
  • 1
  • 10

2 Answers2

2

If the text files are not excessively large you can simplify the task by just reading them all at once.

Use ReadAllLines to extract all lines into an array:

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"))

Then just iterate:

For i = 0 To Math.Min(Names.Count, Ages.Count) - 1
   'Add Names(i) and Ages(i) to database
Next
Jens
  • 6,275
  • 2
  • 25
  • 51
  • so this means that i could only use this code for not so large data? what if there are 50 lines or more on each file, can i use this method too? – tris Jan 20 '15 at 18:01
  • Yes. If your text files go into the several hundred megabyte range, you should start to worry and carfully evaluate performance and ressources but it's no problem in most cases. – Jens Jan 20 '15 at 18:19
  • what will i do if i want it to display first on the textbox? every time it reads each line? – tris Jan 21 '15 at 14:29
0

Open both files and do in a loop:

Dim nameReader, ageReader As String

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

EDIT

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

Add a backgroundworker to your form. In your form load event:

BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()

Add these events:

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
  • i tried combining both but my problem was it will give me some error like "there is already an open streamreader" or something like that :( – tris Jan 20 '15 at 18:02
  • hi i've already tried your code but i won't display each of the line.. it will just display the last value.. – tris Jan 21 '15 at 14:24
  • @tris If you add after `TextBox1.Text = nameReader TextBox2.Text = ageReader` this code `TextBox1.Refresh() TextBox2.Refresh()` it will display each line. **But** the changes in textboxes will be too fast. – γηράσκω δ' αεί πολλά διδασκόμε Jan 21 '15 at 14:38
  • @tris If you want to read each line **simultaneously** and have a small delay in between without hagging your application you need to create a thread or a background worker and send messages to the main thread of the text to be displayed in textboxes. You can't access textboxes in a different thread. The keyword is **simultaneously**. This is the only way to do it. – γηράσκω δ' αεί πολλά διδασκόμε Jan 21 '15 at 15:01
  • i've tried putting the refresh after the textboxes but it's still the same display.. – tris Jan 21 '15 at 15:29