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 BinaryReader Example

Use the BinaryReader class to read a binary file. Call ReadInt32 and ReadString.
BinaryReader reads in a binary file. With the BinaryReader type, we read in each integer, byte, string or other value from a binary file. This type allows us to encode our data in the most efficient way. It is also easy to use.BinaryWriterFile
Example. First, the example opens a file called "file.bin" which is in the current directory. If you have no file there, you can create one with the BinaryWriter type, also found in System.IO.

Next: To use the BinaryReader you want to enclose it in a Using statement, which ensures correct disposal of system resources.

So: We acquire the length of the file, then simply loop through every four bytes using a While-loop, calling ReadInt32 each time.

Tip: You can see that 12 integers we read from the file. We see these are the same integers that were written there.

Integer

Thus: The BinaryWriter and BinaryReader types are used together for perfect compatibility.

VB.NET program that uses BinaryReader Imports System.IO Module Module1 Sub Main() ' Create the reader in a Using statement. ' ... Use File.Open to open the existing binary file. Using reader As New BinaryReader(File.Open("file.bin", FileMode.Open)) ' Loop through length of file. Dim pos As Integer = 0 Dim length As Integer = reader.BaseStream.Length While pos < length ' Read the integer. Dim value As Integer = reader.ReadInt32() ' Write to screen. Console.WriteLine(value) ' Add length of integer in bytes to position. pos += 4 End While End Using End Sub End Module Output 1 4 6 7 11 55 777 23 266 44 82 93
Also, the BinaryReader can read many kinds of data from a file. It handles Strings, with ReadString. It handles many numeric types, not just Integers. These include Double and Long.
Summary. The BinaryReader type is exceedingly useful. It loads binary data in specific file formats into a VB.NET program. This website uses BinaryReader to load all of its data when it starts up.

Also: Because it is based on streams, it uses somewhat less memory as well. It does not need to load an entire file in at once.

© 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