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
Var f As FolderItem = SpecialFolder.Documents.Child("MyFile.data")
Var bStream As BinaryStream
bStream = BinaryStream.Open(f, BinaryStream.LockModes.Read)

Var 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

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

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

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