Xojo.IO.BinaryStream.Open

From Xojo Documentation

Shared Method

Opens the passed FolderItem as a binary stream with the specified lock mode. If an error occurs, an IOException is raised.

Sample Code

Read all the data from a file:

Using Xojo.Core
Using Xojo.IO
Dim f As FolderItem = SpecialFolder.Documents.Child("MyFile.data")
Dim bStream As BinaryStream
bStream = BinaryStream.Open(f, BinaryStream.LockModes.Read)

Dim data As MemoryBlock
data = bStream.Read(bStream.Length)

bStream.Close

To append data to a binary file, open the file in read/write mode and then call the Write method once you have reached EOF:

Using Xojo.IO
Using Xojo.Core

Dim f As FolderItem = SpecialFolder.Documents.Child("MyFile.data")
Dim bStream As BinaryStream
bStream = BinaryStream.Open(f, BinaryStream.LockModes.ReadWrite)

Dim data As MemoryBlock
data = bStream.Read(bStream.Length) // reached EOF

Dim mb As MemoryBlock
Dim t As Text = "Text to append"
mb = TextEncoding.UTF8.ConvertTextToData(t)
bStream.Write(mb) // data is appended to the BinaryStream
bStream.Close