VB.Net program to read all lines from a file using StreamReader class

By Nidhi Last Updated : November 16, 2024

Reading all lines from a file using StreamReader in VB.Net

Here, we will ReadLine() method of StringReader class to read all lines from a text file and then print saved information on the console screen.

Program/Source Code:

The source code to read all lines from a file using StreamReader class is given below. The given program is compiled and executed successfully.

VB.Net code to read all lines from a file using StreamReader class

'Vb.Net program to read all lines from a file 
'using StreamReader class.

Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        Dim stream As New FileStream("C:\data.txt", FileMode.Open)
        Dim line As String
        Dim flag As Boolean = True
        Dim reader As StreamReader
        reader = New StreamReader(stream, Encoding.UTF8)

        Console.WriteLine("Content of file: ")
        While (flag = True)
            line = reader.ReadLine()
            If (line = Nothing) Then
                flag = False
            Else
                Console.WriteLine(vbTab & line)
            End If
        End While
    End Sub
End Module

Output

Content of file:
        It is a book.
        It is a table.
        It is a fan.
Press any key to continue . . .

Explanation

In the above program, we created a class module Module1 that a Main() function. The Main() method is the entry point of the program, here we read all lines of the "C:\data.txt" file using ReadLine() method of StringReader class and then print all lines on the console screen.

VB.Net StringReader, StringWriter, Stream Programs »


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.