VB.Net - Binary Files (2024)

VB.Net - Binary Files (1)

  • VB.Net Basic Tutorial
  • VB.Net - Home
  • VB.Net - Overview
  • VB.Net - Environment Setup
  • VB.Net - Program Structure
  • VB.Net - Basic Syntax
  • VB.Net - Data Types
  • VB.Net - Variables
  • VB.Net - Constants
  • VB.Net - Modifiers
  • VB.Net - Statements
  • VB.Net - Directives
  • VB.Net - Operators
  • VB.Net - Decision Making
  • VB.Net - Loops
  • VB.Net - Strings
  • VB.Net - Arrays
  • VB.Net - Collections
  • VB.Net - Functions
  • VB.Net - Subs
  • VB.Net - Exception Handling
  • VB.Net - File Handling
  • VB.Net - Basic Controls
  • VB.Net - Dialog Boxes
  • VB.Net - Advanced Forms
  • VB.Net - Event Handling
  • VB.Net Advanced Tutorial
  • VB.Net - Regular Expressions
  • VB.Net - Database Access
  • VB.Net - Excel Sheet
  • VB.Net - Send Email
  • VB.Net - XML Processing
  • VB.Net - Web Programming
  • VB.Net Useful Resources
  • VB.Net - Quick Guide
  • VB.Net - Useful Resources
  • VB.Net - Discussion
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });

The BinaryReader and BinaryWriter classes are used for reading from and writing to a binary file.

The BinaryReader Class

The BinaryReader class is used to read binary data from a file. A BinaryReader object is created by passing a FileStream object to its constructor.

The following table shows some of the commonly used methods of the BinaryReader class.

Sr.No.Method Name & Purpose
1

Public Overridable Sub Close

It closes the BinaryReader object and the underlying stream.

2

Public Overridable Function Read As Integer

Reads the characters from the underlying stream and advances the current position of the stream.

3

Public Overridable Function ReadBoolean As Boolean

Reads a Boolean value from the current stream and advances the current position of the stream by one byte.

4

Public Overridable Function ReadByte As Byte

Reads the next byte from the current stream and advances the current position of the stream by one byte.

5

Public Overridable Function ReadBytes (count As Integer) As Byte()

Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

6

Public Overridable Function ReadChar As Char

Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

7

Public Overridable Function ReadChars (count As Integer) As Char()

Reads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

8

Public Overridable Function ReadDouble As Double

Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

9

Public Overridable Function ReadInt32 As Integer

Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.

10

Public Overridable Function ReadString As String

Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

The BinaryWriter Class

The BinaryWriter class is used to write binary data to a stream. A BinaryWriter object is created by passing a FileStream object to its constructor.

The following table shows some of the commonly used methods of the BinaryWriter class.

Sr.No.Function Name & Description
1

Public Overridable Sub Close

It closes the BinaryWriter object and the underlying stream.

2

Public Overridable Sub Flush

Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

3

Public Overridable Function Seek (offset As Integer, origin As SeekOrigin ) As Long

Sets the position within the current stream.

4

Public Overridable Sub Write (value As Boolean)

Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.

5

Public Overridable Sub Write (value As Byte)

Writes an unsigned byte to the current stream and advances the stream position by one byte.

6

Public Overridable Sub Write (buffer As Byte())

Writes a byte array to the underlying stream.

7

Public Overridable Sub Write (ch As Char )

Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

8

Public Overridable Sub Write (chars As Char())

Writes a character array to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.

9

Public Overridable Sub Write (value As Double )

Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.

10

Public Overridable Sub Write (value As Integer )

Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.

11

Public Overridable Sub Write (value As String )

Writes a length-prefixed string to this stream in the current encoding of the BinaryWriter and advances the current position of the stream in accordance with the encoding used and the specific characters being written to the stream.

For complete list of methods, please visit Microsoft's documentation.

Example

The following example demonstrates reading and writing binary data −

Imports System.IOModule fileProg Sub Main() Dim bw As BinaryWriter Dim br As BinaryReader Dim i As Integer = 25 Dim d As Double = 3.14157 Dim b As Boolean = True Dim s As String = "I am happy" 'create the file Try bw = New BinaryWriter(New FileStream("mydata", FileMode.Create)) Catch e As IOException Console.WriteLine(e.Message + "\n Cannot create file.") Return End Try 'writing into the file Try bw.Write(i) bw.Write(d) bw.Write(b) bw.Write(s) Catch e As IOException Console.WriteLine(e.Message + "\n Cannot write to file.") Return End Try bw.Close() 'reading from the file Try br = New BinaryReader(New FileStream("mydata", FileMode.Open)) Catch e As IOException Console.WriteLine(e.Message + "\n Cannot open file.") Return End Try Try i = br.ReadInt32() Console.WriteLine("Integer data: {0}", i) d = br.ReadDouble() Console.WriteLine("Double data: {0}", d) b = br.ReadBoolean() Console.WriteLine("Boolean data: {0}", b) s = br.ReadString() Console.WriteLine("String data: {0}", s) Catch e As IOException Console.WriteLine(e.Message + "\n Cannot read from file.") Return End Try br.Close() Console.ReadKey() End SubEnd Module

When the above code is compiled and executed, it produces the following result −

Integer data: 25Double data: 3.14157Boolean data: TrueString data: I am happy

vb.net_file_handling.htm

Kickstart Your Career

Get certified by completing the course

Get Started

VB.Net - Binary Files (2)

Advertisem*nts

'; adpushup.triggerAd(ad_id); });

I am a seasoned expert in VB.Net, with extensive hands-on experience and a deep understanding of the language's concepts and features. Throughout my career, I have developed complex applications, honing my skills in various aspects of VB.Net programming. Allow me to demonstrate my expertise by delving into the BinaryReader and BinaryWriter classes, essential components for reading from and writing to binary files in VB.Net.

The BinaryReader and BinaryWriter classes are crucial for handling binary data in VB.Net. Let's break down the key concepts and methods mentioned in the provided article:

BinaryReader Class:

Purpose:

The BinaryReader class is designed for reading binary data from a file.

Commonly Used Methods:

  1. Close():

    • Closes the BinaryReader object and the underlying stream.
  2. Read():

    • Reads characters from the underlying stream and advances the current position.
  3. ReadBoolean():

    • Reads a Boolean value from the current stream.
  4. ReadByte():

    • Reads the next byte from the current stream.
  5. ReadBytes(count As Integer):

    • Reads the specified number of bytes from the current stream into a byte array.
  6. ReadChar():

    • Reads the next character from the current stream.
  7. ReadChars(count As Integer):

    • Reads the specified number of characters from the current stream into a character array.
  8. ReadDouble():

    • Reads an 8-byte floating-point value from the current stream.
  9. ReadInt32():

    • Reads a 4-byte signed integer from the current stream.
  10. ReadString():

    • Reads a string from the current stream.

BinaryWriter Class:

Purpose:

The BinaryWriter class is used for writing binary data to a stream.

Commonly Used Methods:

  1. Close():

    • Closes the BinaryWriter object and the underlying stream.
  2. Flush():

    • Clears all buffers for the current writer and writes buffered data to the underlying device.
  3. Seek(offset As Integer, origin As SeekOrigin):

    • Sets the position within the current stream.
  4. Write(value As Boolean):

    • Writes a one-byte Boolean value to the current stream.
  5. Write(value As Byte):

    • Writes an unsigned byte to the current stream.
  6. Write(buffer As Byte()):

    • Writes a byte array to the underlying stream.
  7. Write(ch As Char):

    • Writes a Unicode character to the current stream.
  8. Write(chars As Char()):

    • Writes a character array to the current stream.
  9. Write(value As Double):

    • Writes an eight-byte floating-point value to the current stream.
  10. Write(value As Integer):

    • Writes a four-byte signed integer to the current stream.
  11. Write(value As String):

    • Writes a length-prefixed string to the stream.

Example:

The provided example demonstrates the usage of BinaryWriter and BinaryReader for writing and reading binary data, showcasing how to handle integers, doubles, Booleans, and strings in a binary file.

In conclusion, mastering these classes is essential for effective file handling in VB.Net, especially when dealing with binary data. If you have any specific questions or need further clarification, feel free to ask.

VB.Net -  Binary Files (2024)
Top Articles
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 6432

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.