VB.Net program to read the content of a file character by character

Here, we are going to learn how to read the content of a file character by character in VB.Net?
Submitted by Nidhi, on January 06, 2021

Here, we will use ReadByte() method to read the data from the file byte-wise and then covert each byte into a printable character and print the data on the console screen.

Program/Source Code:

The source code to read the content of a file character by character is given below. The given program is compiled and executed successfully.

'Vb.Net program to read data from file 
'character by character till the end of the file.

Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        Dim stream As New FileStream("C:\data.txt", FileMode.Open)
        Dim val As Integer
        Dim ch As Char
        Dim flag As Boolean = True
        Console.WriteLine("Content of file: ")
        While (flag = True)
            val = stream.ReadByte()

            If (val < 0) Then
                flag = False
            Else
                ch = Convert.ToChar(val)
                Console.Write(ch)
            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 used ReadByte() method to read the data from the specified file byte-wise and then converted each byte into a printable character and print the data on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.