FileStreams - Reading and Writing Files

Objectives


Discussion

Processing Files

Reading Sequential Text Files Using Stream Reader

Dim srF As StreamReader
srF = New StreamReader("filepath ")
Dim sText As String
While (srF.Peek > -1) 
   sText =  srF.ReadLine
End While
srF.Close()

Writing Sequential Text Files Using Stream Writer

Dim swWriter As New StreamWriter("filepath ", True)
swWriter.WriteLine("HI")
swWriter.Close()

Back to top


Demonstration

In this demo we will read and write files.

1.  Create a new project - Unit 7.  Add the following to the form: two buttons (btnRead and btnWrite) and a textbox (txtFile ).  We want the textbox to permit multiple lines so change the multiline property to "True" and the Scrollbars property to "Both".  Also, add the following statement to the top of the code window for the form to import the Namespace that the StreamReader and StreamWriter objects are in:

Imports System.IO

2.  We want to be able to write the contents of txtFile to a file when the user clicks btnWrite.  You can write to any file you want.  Just make sure that you specify a valid path.  Your path depends on your specific computer.  For this example we write to "C:\test.txt". You may have to use another drive or filename.  Note that if the file does not exist, it will be created when you use the StreamWriter object.  Add the following code to the click event for btnWrite:

Dim swWriter As New StreamWriter("c:\test.txt", False)
swWriter.WriteLine(txtFile.Text)
swWriter.Close()

3.  You should type in the first line to see what arguments are needed when you call the StreamWriter constructor.  You will see that the "False" argument specifies you will append the file.  If you append the file then whatever you write gets added to the end of the file and anything in the file remains.  If you do not append, then you write over anything that is in the file.  Also, if no file exists, then a file is created for you.  Also you should type in the variable name "swWriter" to see what methods and properties are available. 

4.  Run your program. Type something in the textbox and click the button.  Then open the file using Notepad. 

5.  Now we will add the code to make the Read button work.  In the click event for btnRead add the following code:

txtFile.Clear()
Dim srF As StreamReader
srF = New StreamReader("C:\test.txt")
While (srF.Peek > -1)
   txtFile.Text = txtFile.Text + srF.ReadLine + vbCrLf
End While
srF.Close()

6.  This code opens and reads the file specified in the constructor for the StreamReader.  Notice the While Loop.  The Peek method peeks ahead in the file to determine if there is anything to read.  The Peek method returns "-1" if there is nothing else to read. Then each line in the file is read using the ReadLine method and added to the textbox.

7.  Run the program and click the read button.   Hopefully the text from your file will appear in the textbox. Experiment.

8.  Now experiment with the code for writing the file.  Change the "False" to "True".  Run the program and experiment.  Change the WriteLine method to Write.  Run the program and experiment.

9.  Experiment with the code for reading the file as well.

Back to top
Exercises

1.  Create a program that will let a user create a file of usernames and passwords.  Your program should meet the following requirements:

  1. It should provide a means for the user to enter a new username and a password for that user.
  2. It should allow the user to add the new username and password to a file of names and passwords. 
  3. It should verify that the user has entered a valid username and password.  At a minimum you should ensure that the user has entered something for both the username and password.  You may also  want to require the password to have a certain number of characters. 

2.  Create a program that reads in the file containing the usernames and passwords and displays the usernames in one listbox and the passwords in another listbox.

3.  Repeat #2 but read the usernames into one array and the passwords into another array and then use the DisplayArray sub to display the usernames and passwords to verify that they have been read in correctly.

4.  [Optional]   Modify #1 so that the user can not add a username that already exists.  To do this you will have to first read in the file and check if the username that the user enters is not already in the file.

Back to top
Links & Help
Back to top