VB.Net program to read all lines of a text file

Here, we are going to learn how to read all lines of a text file in VB.Net?
Submitted by Nidhi, on January 16, 2021

Here, we will use the ReadAllLines() method of the File class to read all lines from the text file.

Program/Source Code:

The source code to read all lines of a text file is given below. The given program is compiled and executed successfully.

'VB.Net program to read all lines of a text file.

Imports System.IO

Module Module1
    Sub Main()
            Dim line(5) As String

            line = File.ReadAllLines("sample.txt")

            For i = 0 To line.Length - 1 Step 1

        Catch ex As FileNotFoundException
            Console.WriteLine("File does not exist")
        End Try
    End Sub
End Module


Person1: hello, how are you?
Person2: I am fine, what about you?
Person1: I am also fine, you are looking handsome.
Person2: Thanks.
Press any key to continue . . .


In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program, here, we created an array of strings that may contain 5 string elements.

line = File.ReadAllLines("sample.txt")

In the above code, we read all lines from the "sample.txt" file and return the array of string, which is assigned to the line and then we printed the data on the console screen.

VB.Net File Handling Programs »



Comments and Discussions!





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

© some rights reserved.