Visual Basic.NET 2008 > Accessing Folders and Files

<< Previous Page Next Page >>

The StreamReader Class in Visual Basic 2008

The StreamReader class provides the necessary methods for reading from a text file and exposes methods that match those of the StreamWriter class (the Write and WriteLine methods). The StreamReader class's constructor is overloaded. You can specify the FileStream object it will use to read data from the file, the encoding scheme, and the buffer size. The simplest form of the constructor is the following:

Dim SR As New StreamReader(FS)

This declaration associates the SR variable with the file on which the FS FileStream object was created. This is the most common form of the StreamReader class's constructor. To prepare your application for reading the contents of the file C:\My Documents\Meeting.txt, use the following statements:

Dim FS As FileStream
Dim SR As StreamReader
FS = New FileStream("c:\My Documents\Meeting.txt",
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write)
SR = New StreamReader(FS)

You can also create a new StreamReader object directly on a file, with the following form of the constructor:

Dim SR As New StreamReader(path)

With both forms of the constructor, you can specify the character encoding with a second argument, as well as a third argument that determines the size of the buffer to be used for the IO operations.

Methods

The StreamReader class provides the following methods for writing data to the underlying file.

Close

The Close method closes the current instance of the StreamReader class and releases any system resources associated with this object.

Peek

The Peek method returns the next character as an integer value, without actually removing it from the input stream. The Peek method doesn't change the current position in the stream. If there are no more characters left in the stream, the value −1 is returned. The Peek method will also return −1 if the current stream doesn't allow peeking.

Read

This method reads a number of characters from the StreamReader class to which it's applied and returns the number of characters read. This value is usually the same as the number of characters you specified unless there aren't as many characters in the file. If you have reached the end of the stream (which is the end of the file), the method returns the value −1. The syntax of the Read method is as follows, where count is the number of characters to be read, starting at the startIndex location in the file:

charsRead = SR.Read(chars, startIndex, count)

The characters are stored in the chars array of characters, starting at the index specified by the second argument. A simpler form of the Read method reads the next character from the stream and returns it as an integer value, where SR is a properly declared StreamReader class:

Dim newChar As Integer
newChar = SR.Read()

ReadBlock

This method reads a number of characters from a text file and stores them in an array of characters. It accepts the same arguments as the Read method and returns the number of characters read.

Dim chars(count - 1) As Char
charsRead = SR.Read(chars, startIndex, count)

ReadLine

This method reads the next line from the text file associated with the StreamReader class and returns a string. If you're at the end of the file, the method returns the Null value. The syntax of the ReadLine method is the following:

Dim txtLine As String
txtLine = SR.ReadLine()

A text line is a sequence of characters followed by a carriage return (\r), line feed (\n), or carriage return and line feed (\r\n). Notice that the NewLine character you might have specified for the specific file with the StreamWriter class is ignored by the ReadLine method. The string returned by the method doesn't include the line terminator.

ReadToEnd

The last method for reading characters from a text file reads all the characters from the current position to the end of the file. We usually call this method once to read the entire file with a single statement and store its contents to a string variable. The syntax of the ReadToEnd method is as follows:

allText = SR.ReadToEnd()

To make sure you're reading the entire file with the ReadToEnd method, reposition the file pointer at the beginning of the file with the Seek method of the underlying stream before calling the ReadToEnd method of the StreamReader class:

FS.Seek (0, SeekOrigin.Begin)

<< Previous Page Next Page >>

 

Table of Contents

  1. Accessing Folders and Files
  2. The IONamespace and the FileSystem Component
  3. Using the My.Computer.FileSystem Component
  4. Manipulating Folders and Files with the IO Namespace
  5. System.IO.Directory class
  6. System.IO.File class
  7. DriveInfo, DirectoryInfo, and FileInfo classes
  8. The Path Class of System.IO namespace
  9. The CustomExplorer Example Application
  10. Accessing Files
  11. The FileStream Class
  12. The StreamWriter Class
  13. The StreamReader Class
  14. Sending Data to a File
  15. The BinaryWriter Class
  16. The BinaryReader Class
  17. The RecordSave Example Application
  18. The FileSystemWatcher Component
  19. The FileSystemWatcher Example Application

     
 
W3computing.com Copyright 2011 All Rights Reserved
 
  Home | Useful links | Contact us | Privacy Policy