The File and StreamReader Classes

   

The File and StreamReader Classes

The Directory class offers a lot to developers. Now that we've covered that, we'll move on to another important class. The File class aids in the creation of FileStream objects and provides routines for the creation, copying, deletion, moving, and opening of files.

The StreamReader class implements a TextReader that reads characters from a byte stream in a particular encoding. StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output.

StreamReader defaults to UTF-8 encoding, rather than the ANSI code page for the current system, unless specified otherwise . UTF-8 handles Unicode characters correctly and gives consistent results on localized versions of the operating system.

This section examines reading (or opening) files from disk and displaying their contents to users. Of course, there are plenty of other reasons to read from disk, but this gets you started with easy-to-understand examples.

Opening a File

The first thing you must do to read data from a file is open it. The File class's OpenText() method is used in the examples that come later in this section. This method returns a StreamReader class, which can then be used to read the data in.

The following code shows how to open a file using the File.OpenText() method:

 Dim reader as StreamReader  Dim strFilename as String  strFilename = "C:\Temp\Dir\Data.txt"  reader = File.OpenText( strFilename ) 

Reading Data

As mentioned earlier, the StreamReader class provides an easy way to read data from a disk file. The two methods used in the following examples are Peek() and ReadLine() . The Peek() method returns a -1 if no more data is left to read. The ReadLine() method returns the next line from the text file.

The following code shows how to use an already opened StreamReader object to read all the lines of a text file:

 While reader.Peek() <> -1      StrAllData = strAllData + reader.ReadLine()  End While 

Figure 5.8 shows a sample application that reads and displays one of two text files (The Bat and the Weasel or The Wolf and the Lamb). If you click on the button labeled Read The Bat and the Weasel, the text from that fable will be displayed. If you click on the button labeled Read The Wolf and the Lamb, the text from that fable will be displayed. If you click on the Read What the Last Visitor Said button, the text that the last visitor entered will be displayed. This example can be found on the book's Web site at http://www.usingasp.net/SysIO/file.aspx. Listing 5.8 shows the code that responds to the button clicks and loads and displays the text data.

Figure 5.8. The File and StreamReader classes make reading text files easy.

graphics/05fig08.gif

Listing 5.8 Using Two Methods to Provide the Functionality that Loads and Displays Text Files
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _    Handles Button1.Click      Dim reader As StreamReader      Try          Label1.Text = ""          reader = file.OpenText(Request.MapPath("BatAndWeasel.txt"))          While reader.Peek() <> -1              Label1.Text = Label1.Text + reader.ReadLine() + "<br>" + vbCrLf          End While      Catch ex As Exception          Label1.Text = ex.Message      End Try  End Sub  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _    Handles Button2.Click      Dim reader As StreamReader      Try          Label1.Text = ""          reader = File.OpenText(Request.MapPath("WolfAndLamb.txt"))          While reader.Peek() <> -1              Label1.Text = Label1.Text + reader.ReadLine() + "<br>" + vbCrLf          End While      Catch ex As Exception          Label1.Text = ex.Message      End Try  End Sub 

Visitor Comments

I created a third button for the preceding example. When it's clicked, the application loads a text file named MessageText.txt. This text file is written in an example that comes later in this chapter. It enables users to write a comment for other users to read. The code can be seen in Listing 5.9.

Listing 5.9 Loading the MessageText.txt File That Is Saved by Visiting Users
 Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)      Dim reader As StreamReader      Try          label1.Text = ""          reader = File.OpenText(Request.MapPath("VisitorFiles\MessageText.txt"))          Dim strTemp As String          strTemp = reader.ReadToEnd()          strTemp = Replace(strTemp, Chr(13) + Chr(10), _              "<br>" + Chr(13) + Chr(10))          label1.Text = strTemp          reader.Close()      Catch ex As Exception          label1.Text = ex.Message      End Try  End Sub 
   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net