How do I... Read Text Files in VBCorLib?
Reading information from files is a common task. VBCorLib provides many facilities
for reading files. One of the most common reasons to read a file
is to read the text within. A text file is a series of bytes that represent text
that is encoded using a specific Encoding. To read the encoded bytes of text,
they must be decoded using an appropriate Encoding back into the original text.
This is what the StreamReader does. A StreamReader takes a series of bytes from a
Stream (such as a FileStream) and decodes them using an Encoding,
returning the decoded text in one of many methods supplied by StreamReader.
By default a StreamReader uses an Encoding returned by Encoding.UTF8.
VBCorLib provides several methods for opening a file to be read from. The simplest way to open a
StreamReader for reading a file is directly creating the object, passing
in the filename.
Dim reader As StreamReader
' create a StreamReader directly, passing in the file name.
Set reader = NewStreamReader("lines.txt")
This will open an existing file using the default encoding Encoding.UTF8.
If the file does not exist, then a FileNotFoundException will be thrown.
Since the file is to be read, then it makes sence that the file must exist for
a StreamReader to be used.
The File class provides many methods for opening a file for reading. For
this specific instance, using the File.OpenText method is suitable. This method
returns a StreamReader object for reading from the specified file.
Dim reader As StreamReader
' Opens a reader the same as NewStreamReader does,
' but uses one of the File methods.
Set reader = File.OpenText("lines.txt")
This method of opening a file for reading is identical to the previous example of
calling NewStreamReader directly. File.OpenText merely wraps an
identical call as the previous example.
The two previous examples show how to open a file directly with a file name.
Sometimes a file may already be open as a FileStream, or may need to be
opened in a more custom manor. A StreamReader can accept a FileStream
object (or any object implementing the Stream interface) inplace of the file name.
Dim reader As StreamReader
' Opens a reader using a FileStream object returned from File.OpenRead.
Set reader = NewStreamReader(File.OpenRead("lines.txt"))
This opens a FileStream in read-only mode. The FileStream is then
passed into the NewStreamReader method. The StreamReader is now
ready for reading from the supplied FileStream.
Once a StreamReader is open, there are many methods provided to retrieve
the text from the file. The lines of a file can be read using the ReadLine
method. This method reads characters until a new-line character is read in. The
returned line does not include the new-line character. The StreamReader
defines both a Carriage-Return and a Line-Feed as valid new-line characters. If a
Carriage-Return is immediately followed by a Line-Feed, then the Line-Feed is also
consumed and not returned.
Dim reader As StreamReader
Dim strLine As String
Dim lines As ArrayList
Set reader = NewStreamReader("lines.txt")
Set lines = New ArrayList
Do
strLine = reader.ReadLine
' ReadLine will return a null String if there is nothing left to read from the Stream.
If cString.IsNull(strLine) Then Exit Do
lines.Add strLine
Loop
reader.CloseReader
This example opens a file containing lines of text separated by either a Carriage-Return or
Line-Feed. The StreamReader is then used to read each line individually and
adds them to an ArrayList. Something to note is that strLine is defined as a
Variant datatype. This is because the StreamReader.ReadLine will return a Null
if there is nothing remaining in the Stream to be read. Once the lines have
been read from the file and the loop is exited, the StreamReader.CloseReader
is called. This will close the underlying Stream and release any resources.
Summary
There are many ways to create a new StreamReader. Each provides a methods
for opening a file in a specific way. Once a StreamReader is created, it
provides many methods for retrieving decoded text from the stream being read from.
|