Buat dan Baca Binary File VB.NET

Read Write Binary File - Sesi belajar vb.net khusus pemula kali ini tentang bagaimana cara check sebuah file dalam direktori apakah ada atau tidak, Membaca dan menulis file binary serta membaca dari sebuah file banary dan membaca file secara berurutan. Dimana tutorial vb.net sudah banyak kami bahas di blog sector code ini, lihat selengkapnya di halaman Tutorial Vb.NET Khusus Pemula.

Untuk mempermudah dalam belajar vb.net ini langsung saja buat project baru menggunakan visual studio dan beri nama "FileDirectory", serta ikuti dan tuliskan code - code dibawah ini :
Buat dan Baca Binary File VB.NET

Buat Function Check File

    Public Shared Function CariData(ByVal _
Data1 As String, ByVal Data2 As String) _
As Boolean
Dim BoolCek As Boolean = True
Dim objData As IO.FileStream
Dim objData2 As IO.FileStream
Dim intData1 As Integer
Dim intData2 As Integer

objData = New IO.FileStream(Data1, IO.FileMode.Open)
objData2 = New IO.FileStream(Data2, IO.FileMode.Open)

If (objData.Length <> objData2.Length) Then
BoolCek = False
Else
Do
intData1 = objData.ReadByte()
intData2 = objData2.ReadByte()
If intData1 <> intData2 Then
BoolCek = False
Exit Do
End If
Loop While (intData1 <> -1)
End If
objData.Close()
objData2.Close()
Return BoolCek
End Function

Panggil Function dari Sub main

    Public Shared Sub Main()
If CariData("scqq.png", "ganteng.png") Then
Console.WriteLine("File ditemukan !")
Console.ReadKey()
Else
Console.WriteLine("File tidak ditemukan !")
Console.ReadKey()
End If
End Sub

Sehingga keseluruhan file Check File :

Public Class Class1
Public Shared Function CariData(ByVal _
Data1 As String, ByVal Data2 As String) _
As Boolean
Dim BoolCek As Boolean = True
Dim objData As IO.FileStream
Dim objData2 As IO.FileStream
Dim intData1 As Integer
Dim intData2 As Integer

objData = New IO.FileStream(Data1, IO.FileMode.Open)
objData2 = New IO.FileStream(Data2, IO.FileMode.Open)

If (objData.Length <> objData2.Length) Then
BoolCek = False
Else
Do
intData1 = objData.ReadByte()
intData2 = objData2.ReadByte()
If intData1 <> intData2 Then
BoolCek = False
Exit Do
End If
Loop While (intData1 <> -1)
End If
objData.Close()
objData2.Close()
Return BoolCek
End Function
Public Shared Sub Main()
If CariData("scqq.png", "ganteng.png") Then
Console.WriteLine("File ditemukan !")
Console.ReadKey()
Else
Console.WriteLine("File tidak ditemukan !")
Console.ReadKey()
End If
End Sub

End Class

Penjelasan :

Buatlah file gambar (format .png) dalam folder start up aplikasi yang mana berada di [project]\bin\Debug\namafile.jpg, saat aplikasi dijalankan maka akan langsung mengecek file tersebut apakah tersedia atau tidak dan akan memunculkan pesan "File ditemukan !" jika ditemukan.

Baca dan Tulis File Binary

Tuliskan code berikut.

Buat File Binary :

        Dim Stream As FileStream
Try
Stream = New _
FileStream("ganteng.dat", FileMode.Create)
Catch E As Exception
Console.WriteLine("Error " _
+ "saat membuat ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
Dim BinaryStream As BinaryWriter
BinaryStream = New BinaryWriter(Stream)
Dim NamaBlog As String = "Sector Code"
Dim Umur As Integer = 23
Dim Admin As String = "Harison Matondang"
Try
BinaryStream.Write(NamaBlog)
BinaryStream.Write(Umur)
BinaryStream.Write(Admin)
BinaryStream.Close()
Console.WriteLine("Data sudah " _
+ "dibuat di ganteng.dat")
Catch E As Exception
Console.WriteLine("Error saat " _
+ "membuat ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try

Baca File Binary :

        Try
Stream = New FileStream("ganteng.dat",
FileMode.Open)
Catch E As Exception
Console.WriteLine("Error opening ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
Console.ReadLine()
End Try
Dim BinaryStreamReader As New BinaryReader(Stream)
Try
NamaBlog = BinaryStreamReader.ReadString()
Umur = BinaryStreamReader.ReadInt32()
Admin = BinaryStreamReader.ReadString()
BinaryStreamReader.Close()
Console.WriteLine("Nama Blog {0}", NamaBlog)
Console.WriteLine("Umur Admin {0}", Umur)
Console.WriteLine("Nama Admin {0}", Admin)
Console.ReadLine()
Catch E As Exception
Console.WriteLine("Error reading to ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
Console.ReadLine()
End Try

Buat dan Baca File Binary selengkapnya :

Imports System.IO
Module Module1
Sub Main()
Dim Stream As FileStream
Try
Stream = New _
FileStream("ganteng.dat", FileMode.Create)
Catch E As Exception
Console.WriteLine("Error " _
+ "saat membuat ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try
Dim BinaryStream As BinaryWriter
BinaryStream = New BinaryWriter(Stream)
Dim NamaBlog As String = "Sector Code"
Dim Umur As Integer = 23
Dim Admin As String = "Harison Matondang"
Try
BinaryStream.Write(NamaBlog)
BinaryStream.Write(Umur)
BinaryStream.Write(Admin)
BinaryStream.Close()
Console.WriteLine("Data sudah " _
+ "dibuat di ganteng.dat")
Catch E As Exception
Console.WriteLine("Error saat " _
+ "membuat ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
End Try

'baca

Try
Stream = New FileStream("ganteng.dat",
FileMode.Open)
Catch E As Exception
Console.WriteLine("Error opening ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
Console.ReadLine()
End Try
Dim BinaryStreamReader As New BinaryReader(Stream)
Try
NamaBlog = BinaryStreamReader.ReadString()
Umur = BinaryStreamReader.ReadInt32()
Admin = BinaryStreamReader.ReadString()
BinaryStreamReader.Close()
Console.WriteLine("Nama Blog {0}", NamaBlog)
Console.WriteLine("Umur Admin {0}", Umur)
Console.WriteLine("Nama Admin {0}", Admin)
Console.ReadLine()
Catch E As Exception
Console.WriteLine("Error reading to ganteng.Dat")
Console.WriteLine("Error {0}", E.Message)
Console.ReadLine()
End Try
End Sub

End Module

Penjelasan :

Saat pertama kali aplikasi console dijalankan akan langsung membuat file binary berformat .dat "ganteng.dat" yang mempunyai isi file string dan integer :
Nama Blog Sector Code
Umur Admin 23
Nama Admin Harison Matondang
Dan kemudian langsung membaca file .dat tersebut dan memunculkannya di console :
Data sudah dibuat di ganteng.dat
Nama Blog Sector Code
Umur Admin 23
Nama Admin Harison Matondang

Baca File dari File Binary

Module Module1
Sub Main()
Dim Fileemp As New Employee
Dim Nomor As Integer = FreeFile()
FileOpen(Nomor, "ganteng.dat", OpenMode.Random,
OpenAccess.ReadWrite, OpenShare.Shared,
Len(Fileemp))
FilePut(Nomor, New Employee(1, "S", "C"))
FilePut(Nomor, New Employee(2, "E", "O"))
FilePut(Nomor, New Employee(3, "C", "D"))
FilePut(Nomor, New Employee(4, "T", "D"))
FilePut(Nomor, New Employee(5, "O", "E"))
FilePut(Nomor, New Employee(6, "R", "E"))
Dim dATAobj As ValueType = DirectCast(Fileemp, ValueType)
For Each X As Integer In New Integer() {3, 1, 5, 2, 6}
FileGet(Nomor, dATAobj, X)
Fileemp = DirectCast(dATAobj, Employee)
Console.WriteLine(Fileemp.ToString())
Next X
FileClose(Nomor)
Console.ReadLine()
End Sub
Public Structure Employee
Public gETid As Integer
Public Namaawal As String
Public NamaAkhir As String
Public Sub New(ByVal DataID As Integer, ByVal Nama As String,
ByVal Nama2 As String)
gETid = DataID
Namaawal = Nama
NamaAkhir = Nama2
End Sub
Public Overrides Function ToString() As String
Return gETid & ": " & Namaawal & " " & NamaAkhir
End Function
End Structure

End Module