ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

Here, we are going to learn how to read all lines from a file using StreamReader class in VB.Net?
Submitted by Nidhi, on January 06, 2021

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 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 »

ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.