Writes a length-prefixed string
Imports System
Imports System.IO
Imports System.Text
Public Class BinReadWrite
Public Shared Sub Main()
Dim testfile As String = "C:\testfile.bin"
Dim fs As FileStream = File.Create(testfile)
Dim utf8 As New UTF8Encoding()
Dim bw As New BinaryWriter(fs, utf8)
Dim bstring As String
bstring = "this is a test" + vbNewLine
bw.Write(bstring)
fs.Seek(0, SeekOrigin.Begin)
Dim len As Integer = fs.ReadByte() And 127
len = len + fs.ReadByte() * 128
Dim rawbytes(len) As Byte
fs.Read(rawbytes, 0, len)
Console.WriteLine(utf8.GetString(rawbytes))
fs.Seek(0, SeekOrigin.Begin)
Dim br As New BinaryReader(fs, utf8)
bstring = br.ReadString()
Console.WriteLine(bstring)
fs.Close()
End Sub
End Class
Related examples in the same category