- Opening Text Files for Reading
- Reading from an Open Text File
- Opening and Creating Text Files for Writing
- Writing to a Text File
- Summing Up
Reading from an Open Text File
An open text file has a pointer that indicates where the next read operation will occur. When a file is first opened, the pointer is at the beginning of the file. Most read operations automatically advance the pointer so it’s easy to read data sequentially from the beginning to the end of the file. You can read anything from a single character to the entire file contents by using the StreamReader methods described in the following table.
Method |
Description |
Read() |
Reads the next character and returns it as a type Integer. Returns -1 if the end of the stream has been reached. Use the Chr() function to convert the return value to a character. |
ReadLine() |
Reads a line of text from the file and returns it as a type String. A line is defined as all text from the current pointer position to the next carriage return/line feed (CR/LF) combination. ReadLine() returns the special value Nothing if the pointer is at the end of the file. |
ReadBlock(buf(), index, count) |
buf() is an array of type Char. The method counts characters from the file and places them in buf(), starting at array position index. If count is greater than the length of buf() minus index, an ArgumentException is thrown. If fewer than count characters are available in the file, those characters are returned and no exception is thrown. |
ReadToEnd() |
Reads to the end of the file and returns the result as a type String. |
Let’s look at some examples.
This code snippet opens a text file and reads the entire contents into a text box:
Dim sr As StreamReader("data.txt") TextBox1.Text = sr.ReadToEnd() sr.Close()
The following code reads the fifth line of the specified file into the program variable FifthLine. Note that lines 1–4 of the file are read as well, which is required to get to the fifth line, but the data read from those lines ends up being overwritten.
Dim sr As StreamReader("data.txt") For i = 0 to 4 FifthLine = sr.ReadLine() Next i
The following code opens the file and reads characters up to and including the first exclamation point (or end of file, whichever comes first). The resulting string is stored in the variable buf.
sr = File.OpenText("test.txt") Do input = sr.Read() If input = -1 Then Exit Do buf &= Chr(input) If input = Asc("!") Then Exit Do Loop While True sr.Close()
The final text file reading method you should know about is Peek(). Like Read(), it returns the next character as an integer, but Peek() doesn’t advance the stream pointer. It’s useful when you want to read characters up to but not including the first instance of a specified character. For example, we can modify the preceding example to read all characters up to but not including the first exclamation point:
sr = File.OpenText("test.txt") Do input = sr.Peek() If input = Asc("!") Then Exit Do input = sr.Read() If input = -1 Then Exit Do buf &= Chr(input) Loop While True sr.Close()