TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET BinaryWriter Example

Use the BinaryWriter type from System.IO to write a binary file. Call the Write Subroutine.
BinaryWriter creates a binary file. This file contains a certain layout of bytes. With BinaryWriter, we write individual bytes, integers or strings to a file location. This makes possible the creation of files in certain formats.BinaryReaderFile
An example. First, this example creates an array of twelve integers on the managed heap. These integers will be written one-by-one to the file. Next, a Using statement encloses a BinaryWriter instance.

Note: The File.Open method is used to create the "file.bin" file, and this is passed to the BinaryWriter constructor.

Finally: The For Each loop simply writes each element in the array to the file.

For Each, For
VB.NET program that uses BinaryWriter Imports System.IO Module Module1 Sub Main() ' Write this array to the file. Dim array() As Int32 = {1, 4, 6, 7, 11, 55, 777, 23, 266, 44, 82, 93} ' Create the BinaryWriter and use File.Open to create the file. Using writer As BinaryWriter = New BinaryWriter(File.Open("file.bin", FileMode.Create)) ' Write each integer. For Each value As Int32 In array writer.Write(value) Next End Using End Sub End Module
File size. When you run this example, look at the current directory of the executable and check the file size of the "file.bin" file. It will be 48 bytes. This is equal to the length of twelve four-byte integers.

Result: The file "file.bin" is created in the current directory. It contains some data.

BinaryReader. When you use BinaryWriter, you will often also want to use BinaryReader to read in the files you create. The 2 types make a good pair—they are both in System.IO.
Summary. BinaryWriter provides a way to easily create specific file formats and layouts using an imperative programming style. Because you can write parts individually, you do not need to construct the entire result in memory before you begin.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf