Today in VB.NET, I'm going to teach you how to read and view contents of a text file using Binary Reader. I made this tutorial before but it is created using StreamReader. See here:
How to Read and Write Text Files in VB.NET
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to
File, click
New Project, and choose
Windows Application.
2. Next, add an OpenFileDialog named
OpenFileDialog1 for browsing text files, two textboxes named
TextBox1 for displaying the filename of the text file and
TextBox2 for displaying the content of that text file. Insert also two buttons named
Button1 for browsing a text file, and
Button2 for displaying its content. You must design your interface like this:
3. Import the System.IO namespace to access the FileStream and BinaryReader class.
4. For browsing text files, put this code in your Button1.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.Filter = "Text Files (*)|*.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
The code above filters the browsing of file into text files only, meaning the .txt extension file. After choosing the desired text file, then it will display the path into TextBox1.
5. For reading the contents of the text file, put this code in your Button2.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' initialize filestream for accessing text files and BinaryReader for viewing its content
Dim fs As System.IO.FileStream
Dim br As System.IO.BinaryReader
'initialize buffered array and lenth of the file
Dim buffer(100) As Char
Dim mylength As Long
'access the text file in your textbox1 using FileStream
fs = New System.IO.FileStream(TextBox1.Text, IO.FileMode.OpenOrCreate)
'read the content of your textfile in the path inputted
br = New System.IO.BinaryReader(fs)
' make length equal to the text file
mylength = fs.Length
If mylength > 100 Then
mylength = 100
End If
'read the contents of the text file
br.Read(buffer, 0, mylength)
'display the output in textbox2
TextBox2.Text = buffer
'close the file
End Sub
Full source code:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim OpenFileDialog1 As New OpenFileDialog
OpenFileDialog1.Filter = "Text Files (*)|*.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' initialize filestream for accessing text files and BinaryReader for viewing its content
Dim fs As System.IO.FileStream
Dim br As System.IO.BinaryReader
'initialize buffered array and lenth of the file
Dim buffer(100) As Char
Dim mylength As Long
'access the text file in your textbox1 using FileStream
fs = New System.IO.FileStream(TextBox1.Text, IO.FileMode.OpenOrCreate)
'read the content of your textfile in the path inputted
br = New System.IO.BinaryReader(fs)
' make length equal to the text file
mylength = fs.Length
If mylength > 100 Then
mylength = 100
End If
'read the contents of the text file
br.Read(buffer, 0, mylength)
'display the output in textbox2
TextBox2.Text = buffer
'close the file
End Sub
End Class
Output:
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:
[email protected]
Add and Follow me on Facebook:
https://www.facebook.com/donzzsky
Visit and like my page on Facebook at:
https://www.facebook.com/BermzISware