The Stream class is an abstract one, and you can’t use it directly in your code. To prepare your application to write to a file, you must set up a FileStream object, which is the channel between your application and the file. The methods for writing and reading data are provided by the StreamReader/StreamWriter or BinaryReader/BinaryWriter classes, which are created on top of the FileStream object.
The FileStream object’s constructor is overloaded; its most common forms require that you specify the path of the file and the mode in which the file will be opened (for reading, appending, writing, and so on). The simpler form of the constructor is as follows:
Dim FS As New FileStream(path, fileMode)
Code language: PHP (php)
The fileMode argument is a member of the FileMode enumeration (see Table 15.3). It’s the same argument used by the Open method of the File class. Also similar to the Open method of the File class, another overloaded form of the constructor allows you to specify the file’s access mode; the syntax of this method is the following:
Dim FS As New FileStream(path, fileMode, fileAccess)
Code language: PHP (php)
The last argument is a member of the FileAccess enumeration (see Table 15.4). The last overloaded form of the constructor accepts a fourth argument, which determines the file’s sharing mode:
Dim FS As New FileStream(path, fileMode, fileAccess, fileShare)
Code language: PHP (php)
The fileShare argument’s value is a member of the FileShare enumeration (see Table 15.5).
Properties
You can use the following properties of the FileStream object to retrieve information about the underlying file.
CanRead, CanSeek, CanWrite
These three properties are read-only and they determine whether the current stream supports reading, seeking, and writing, respectively. If the file associated with a specific FileStream object can be read, the CanRead property returns True. A seek operation in the context of files doesn’t locate a specific value in the file. It simply moves the current position to any location within the file. The CanWrite property is a True/False value that’s True if the file associated with a specific FileStream object can be written to and False if the file can’t be written.
Length
This read-only property returns the length of the file associated with the FileStream current object in bytes.
Position
This property gets or sets the current position within the stream. You can compare the Position property to the Length property to find out whether you have reached the end of an existing file. When these two properties are equal, there are no more data to read.
Methods
The FileStream object exposes a few methods, which are discussed here. The methods for accessing a file’s contents are discussed in the following section.
Lock
This method allows you to lock the file you’re accessing, or part of it. The syntax of the Lock method is the following, where position is the starting position and length is the length of the range to be locked:
Lock(position, length)
To lock the entire file, use this statement:
FileStream.Lock(1, FileStream.Length)
Code language: CSS (css)
Seek
This method sets the current position in the file represented by the FileStream object:
FileStream.Seek(offset, origin)
Code language: CSS (css)
The new position is offset bytes from the origin. In place of the origin argument, use one of he SeekOrigin enumeration members, listed in Table 11.6.
Table 11.6: SeekOrigin Enumeration
Value | Effect |
---|---|
Begin | The offset is relative to the beginning of the file. |
Current | The offset is relative to the current position in the file. |
End | The offset is relative to the end of the file. |
SetLength
This method sets the length of the file represented by the FileStream object. Use this method after you have written to an existing file to truncate its length. The syntax of the SetLength method is this:
FileStream.SetLength(newLength)
Code language: CSS (css)
If the specified value is less than the length of the file, the file is truncated; otherwise, the file is expanded. To completely overwrite the contents of an existing file, call this method as soon as you open the file to set its length to zero — in effect, initializing the file.