The FileStream class represents a file access stream for reading and writing binary values to a file.
// the file this example creates, in the system's temporary directory
// (Environment.getTempPath() does not include a trailing separator)
const path = `${Environment.getTempPath()}/stream.bin`;
// create a binary file; File.open() returns a FileStream
let stream = File.open(path,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.None);
// make a buffer with some data to write
const buf = new MemoryBuffer(3);
buf[0] = 0;
buf[1] = 1;
buf[2] = 2;
// write the buffer to the stream
stream.write(buf, 0, 3);
// close the stream
stream.close();
// check to make sure the file exists
if (File.exists(path)) {
alert("file created successfully.");
}
// open up the file for reading
stream = File.open(path,
FileMode.Open,
FileAccess.Read,
FileShare.None);
buf.clear();
// read 3 bytes from the stream
stream.read(buf, 0, 3);
stream.close();
alert(`The following value should be 2. It is: ${buf[2]}`); True if the file stream was successfully closed, or false if an error was encountered. If the file stream was not open when close() was called, false is returned.
Calling the close() method will close an open file stream and free resources used during the file input/output operations
The total number of bytes read, or zero if an end of file condition was encountered, or if an error occurred.
Reads the number of bytes specified by the parameter num_bytes into the buffer specified by the buffer parameter. The bytes will be placed in the buffer at the offset specified in the offset parameter. The array needs to be large enough to hold the requested number of bytes, otherwise the call will fail.
Returns true upon success, and false otherwise.
Seeks to the relative position specified by offset. The starting point for the seek is specified by the second parameter. The value of origin can be SeekOrigin.Begin, SeekOrigin.Current, or SeekOrigin.End.
The total number of bytes written by the operation or zero if an error occurred.
Writes the number of bytes specified by the parameter num_bytes from the buffer specified by the buffer parameter. The bytes from the buffer will be written from the offset specified in the offset parameter.