×

VB.Net Programs

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

By Nidhi Last Updated : November 16, 2024

Reading a file character by character in VB.Net

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 code to read the content of a file character by character

'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.

VB.Net StringReader, StringWriter, Stream Programs »


Comments and Discussions!

Load comments ↻





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