Buat dan Baca Binary File VB.NET

Tutorial Belajar VB.NET khusus pemula : read write binary file vb.net, cara membaca dan membuat file binary dan check file folder vb.net, tutorial vb.net selengkapnya di sector code

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

COMMENTS

Next Recomended


Feel free to code it up and send us a pull request.

Hi everyone, let's me know how much this lesson can help your work. Please Subscribe and Follow Our Social Media 'kodeajaib[dot]com' to get Latest tutorials and will be send to your email everyday for free!, Just hit a comment if you have confused. Nice to meet you and Happy coding :) all ^^



Follow by E-Mail


Name

ADO.NET,3,Ajax,6,Android,9,AngularJS,4,ASP.NET,4,Blogger Tutorials,7,Bootstrap,7,C++,1,Codeigniter,2,Cplusplus,6,Crystal Report,6,CSharp,25,Ebook Java,2,FlyExam,1,FSharp,3,Game Development,2,Java,35,JDBC,2,Laravel,84,Lumen,2,MariaDB,2,Ms Access,3,MySQL,31,ODBC,6,OleDB,1,PHP,14,PHP Framework,5,PHP MYSQLI,9,PHP OOP,5,Python,8,Python 3,4,SQL Server,4,SQLite,4,Uncategorized,5,Vb 6,2,Vb.Net,89,Video,48,Vue Js,4,WPF,2,Yii,3,
ltr
item
KODE AJAIB: Buat dan Baca Binary File VB.NET
Buat dan Baca Binary File VB.NET
Tutorial Belajar VB.NET khusus pemula : read write binary file vb.net, cara membaca dan membuat file binary dan check file folder vb.net, tutorial vb.net selengkapnya di sector code
http://1.bp.blogspot.com/-TJFirOereNk/Vkr-3aCOCBI/AAAAAAAABNM/cYhO0uKEfQU/s320/file-binary-vbnet.jpg
http://1.bp.blogspot.com/-TJFirOereNk/Vkr-3aCOCBI/AAAAAAAABNM/cYhO0uKEfQU/s72-c/file-binary-vbnet.jpg
KODE AJAIB
https://www.kodeajaib.com/2015/11/buat-dan-baca-binary-file-vbnet.html
https://www.kodeajaib.com/
https://www.kodeajaib.com/
https://www.kodeajaib.com/2015/11/buat-dan-baca-binary-file-vbnet.html
true
3214704946184383982
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy