5 Replies - 2568 Views - Last Post: 16 March 2013 - 07:00 AM Rate Topic: -----

#1 shah_00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-March 13

How do i read next line after a specific word is read in text file ?

Posted 16 March 2013 - 03:39 AM

I have a text file in my computer, its look like...

Line 1
This is line 1

Line 2
This is line 2

And here is the code :

 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim read As New System.IO.StreamReader("C:\New Text document.txt")

        For a As Integer = 0 To read.EndOfStream
            TextBox1.Text = read.ReadToEnd
        Next 


It give an output as...

Line 1
This is line 1

Line 2
This is line 2

But how do i read line after specific line is read,as example i want it to read "This is line 2" as output after seaching word "Line 1". I know there is method called indexOf, but i actually not really know how to use it.


Is This A Good Question/Topic? 0
  • +

Replies To: How do i read next line after a specific word is read in text file ?

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1533
  • View blog
  • Posts: 4,919
  • Joined: 25-September 09

Re: How do i read next line after a specific word is read in text file ?

Posted 16 March 2013 - 04:30 AM

if you use ReadLine() instead of ReadToEnd, the streamreader will move through the text line by line everytime the code encounters a read.ReadLine()

Then you can look at the contents of each line as it comes in and react
Was This Post Helpful? 0
  • +
  • -

#3 shah_00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-March 13

Re: How do i read next line after a specific word is read in text file ?

Posted 16 March 2013 - 05:34 AM

I found a solution, the code goes like this

 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim read As New System.IO.StreamReader("C:\New Text.txt")
        Dim a As String

        For Each a In read.ReadLine
            If read.ReadLine.Equals("Line 2") Then
                Label1.Text = read.ReadLine()
            End If
        Next

    End Sub 



Output is :
This is Line 2

Its work, but it doesn't read anything if i change "Line 2" to "Line 1". That new problem.

I found a solution, the code goes like this

 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim read As New System.IO.StreamReader("C:\New Text.txt")
        Dim a As String

        For Each a In read.ReadLine
            If read.ReadLine.Equals("Line 2") Then
                Label1.Text = read.ReadLine()
            End If
        Next

    End Sub 



Output is :
This is Line 2

Its work, but it doesn't read anything if i change "Line 2" to "Line 1". That new problem.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 154
  • View blog
  • Posts: 570
  • Joined: 01-February 13

Re: How do i read next line after a specific word is read in text file ?

Posted 16 March 2013 - 05:53 AM

Hello shah_01,
You should set it up to read the file lines with a while statement instead of a For Each loop. Also do not forget to close the streamreader when done reading from it.
        Dim read As New System.IO.StreamReader("C:\New Text.txt")

        While Not read.EndOfStream
            Dim line As String = read.ReadLine
            If line = "Line 1" Then
                Label1.Text = read.ReadLine
            End If
        End While
        read.Close()


:bigsmile:
Was This Post Helpful? 1
  • +
  • -

#5 shah_00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-March 13

Re: How do i read next line after a specific word is read in text file ?

Posted 16 March 2013 - 06:54 AM

That work....thank to all of you....maybe I need to practice more...Thank in advance ! :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 982
  • View blog
  • Posts: 969
  • Joined: 30-September 10

Re: How do i read next line after a specific word is read in text file ?

Posted 16 March 2013 - 07:00 AM

Also, to guarantee that the StreamReader is always cleaned up when you are done with it, you should use a using statement:

Using read As New System.IO.StreamReader("C:\New Text.txt")
    While Not read.EndOfStream
        Dim line As String = read.ReadLine
        If line = "Line 1" Then
            Label1.Text = read.ReadLine
        End If
    End While
End Using



That will automatically call Dispose() on the StreamReader when execution gets to the End Using line. In the case of the StreamReader, Dispose() does the same thing as Close().
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1