BinaryStream.Open

From Xojo Documentation

Shared Method

BinaryStream.Open(f As FolderItem, readwrite As Boolean = False) As BinaryStream

New in 2009r3

Supported for all project types and targets.

Opens the passed FolderItem as a binary stream. The readwrite parameter controls whether the file is open for read/write operations or read-only. When True, you can read and write to the file. When False you can only read from the file. The default is False. If an error occurs, an IOException is raised.

Notes

Call Open using the syntax for a shared method.

Sample Code

This code reads a text file called "Whole File" and displays it in a TextArea. There is no attempt to parse the contents of the file.

Dim f As FolderItem = GetFolderitem("Whole File")
If f <> Nil Then
Try
Dim bs As BinaryStream = BinaryStream.Open(f, False) // Open as read-only
// read the whole binaryStream
TextArea1.Text = bs.Read(bs.Length)

Catch e As IOException
// something went wrong when opening the file. This is where you would handle the error
// if appropriate
End Try
End If

This code reads each pair of bytes from a file and writes them in reverse order to a new file. The user chooses the source file using the Open-file dialog box and saves the new file using the Save as dialog box.

Dim inputFile As FolderItem = GetOpenFolderItem("text")
If inputFile <> Nil Then
// Open as read-only
Dim inputStream As BinaryStream = BinaryStream.Open(inputFile, False)
inputStream.LittleEndian = True

Dim outputFile As FolderItem = GetSaveFolderItem("", "")
If outputFile <> Nil Then
// Open as read-write
Dim outputStream As BinaryStream = BinaryStream.Create(outputFile, True)
Do Until inputStream.EOF
outputStream.WriteInt8(inputStream.ReadInt8)
Loop
End If
End If