From Xojo Documentation
|You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!|
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.
Call Open using the syntax for a shared method.
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.
If f <> Nil Then
Var 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
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.
If inputItem <> Nil Then
Var inputStream As BinaryStream = BinaryStream.Open(f, False)
Var outputItem As FolderItem = FolderItem.ShowSaveFileDialog("","")
If outputItem <> Nil Then
Var outputStream As BinaryStream = BinaryStream.Create(f, True)
outputStream.LittleEndian = Not inputStream.LittleEndian
Do Until inputStream.EndOfFile
If inputStream.Length - inputStream.BytePosition >= 2 Then
Catch exc As IOException
MessageBox("Oops - failed to create the output file.")
Note: Create should here be called with overwrite=True because GetSaveFolderItem already asks the user for permission to do this (if the user doesn't want to overwrite an existing file, GetSaveFolderItem would return nil).