Friday, March 18, 2005
FileStream
FileStream enables manipulating of files via byte access.
FileStream is derived from Stream and contains all Stream’s functionality.
To create a byte straem linked to a file, create a FileStream object:
FileStream(string fileName, FileMode mode)
This constructor will depending on the mode specified, access the file with read / write capability
FileMode
FileMode.Append
Output is appended to the end of file
FileMode.Create
Creates a new output file. Any existing file with the same name will be deleted.
FileMode.CreateNew
Creates a new output file - the file must not already exist. (If the file exist, IOException will be thrown)
FileMode.Open
Opens an existing file
FileMode.OpenOrCreate
Opens a file if it exists, or creates the file if it does not already exist.
FileMode.Truncate
Opens a pre-existing file, but reduces its length to zero.
FileStream(string fileName, FileMode mode, FileAccess how)
The mode specifies how the file will be opened. FileAccess determines how the file can be accessed.
FileAccess.Read
Data can be read from the file
FileAccess.ReadWrite
Data can be written to and read from the file
FileAccess.Write
Data can be written to the file
void FileStream.Close()
When one’s done with the file, the Close() method should be called.
Read from a File
int FileStream.ReadByte()
Each time it is called, a single byte from the file is read and returns it as an integer value. When the end of file is encounted, it returns -1
int FileStream.Read(byte[] buf, int offset, int numBytes)
Read() attempts to read up to numBytes bytes into buf starting at buf[offset]. It retuns the number of bytes successfully read.
Write to a File
void FileStream.WriteByte(byte value)
Writes a byte specified by value to the file. If an error occurs during the write, an IOException will be thrown.
void FileStream.Write(byte[] buf, int offset, int numBytes)
An array of bytes can be written to a file by calling Write(). It writes numBytes bytes from the array buf, beginning at buf[offset] to the file.
void FileStream.Flush()
Output is sometimes not immediately written to the file. It is buffered. To cause data to be written to files whether the buffer is full, call the Flush() method.
Output is
FileStream is derived from Stream and contains all Stream’s functionality.
To create a byte straem linked to a file, create a FileStream object:
FileStream(string fileName, FileMode mode)
This constructor will depending on the mode specified, access the file with read / write capability
FileMode
FileMode.Append
Output is appended to the end of file
FileMode.Create
Creates a new output file. Any existing file with the same name will be deleted.
FileMode.CreateNew
Creates a new output file - the file must not already exist. (If the file exist, IOException will be thrown)
FileMode.Open
Opens an existing file
FileMode.OpenOrCreate
Opens a file if it exists, or creates the file if it does not already exist.
FileMode.Truncate
Opens a pre-existing file, but reduces its length to zero.
FileStream(string fileName, FileMode mode, FileAccess how)
The mode specifies how the file will be opened. FileAccess determines how the file can be accessed.
FileAccess.Read
Data can be read from the file
FileAccess.ReadWrite
Data can be written to and read from the file
FileAccess.Write
Data can be written to the file
void FileStream.Close()
When one’s done with the file, the Close() method should be called.
Read from a File
int FileStream.ReadByte()
Each time it is called, a single byte from the file is read and returns it as an integer value. When the end of file is encounted, it returns -1
int FileStream.Read(byte[] buf, int offset, int numBytes)
Read() attempts to read up to numBytes bytes into buf starting at buf[offset]. It retuns the number of bytes successfully read.
Write to a File
void FileStream.WriteByte(byte value)
Writes a byte specified by value to the file. If an error occurs during the write, an IOException will be thrown.
void FileStream.Write(byte[] buf, int offset, int numBytes)
An array of bytes can be written to a file by calling Write(). It writes numBytes bytes from the array buf, beginning at buf[offset] to the file.
void FileStream.Flush()
Output is sometimes not immediately written to the file. It is buffered. To cause data to be written to files whether the buffer is full, call the Flush() method.
Output is