File IO (Input Output)PowerPoint PresentationSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50File IO (Input Output)Slides 41 to 50 are new! Chapter 9 of the bookRead sections 9.1 and 9.2Dim strLastNames() As String = {"GUNOPULOS", "PApadopoulos", "Keogh", "vlachos"} Dim strFirstNames() As String = {"dima", "JoHn", "Keogh", "MARY"} Dim blnIsMale() As Boolean = {True, True, True, False} Dim sngGPA() As Single = {1.2, 2.4, 4.0, 3.4}Up to this point, we have either hard coded the information our program used (as below), or lost all information once the program closed… Most real programs don’t do this. Typically programs read in information from files, process the information, and write information back to files.Consider a word processor:We might run the Notepad programWe then might open a file, say a homeworkWe might process the file, say edit it, or run a spell checkWhen we are done, we would save the fileWith Visual Basic, we can open all kinds of files, including sound files, movie files, webpages, spreedsheets etc.However, we will only consider simple, structured text filesBy structured I mean that the file has well defined conventions.For example:• One person per row• Exactly 4 fields• Order is, last name, first name, sex, GPA• Missing values are denoted as “@@”• The last name is exactly 25 characters long• Dim strLastNames() As String = {"GUNOPULOS", "PApadopoulos", "Keogh", "vlachos"} Dim strFirstNames() As String = {"dima", "JoHn", "Keogh", "MARY"} Dim blnIsMale() As Boolean = {True, True, True, False} Dim sngGPA() As Single = {1.2, 2.4, 4.0, 3.4}A Padded FileNote that these are spaces, NOT tabsNote that file ends hereC:\Documents and Settings\eamonn.keogh\Desktop\StudentData.txtMy file has a path and n ame. Note that the name includes the extension, in this case “.txt”Imports System.IODim myStream As New FileStream("C:\Documents and Settings\eamonn.keogh\Desktop\StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempStringDim myStream As New FileStream("C:\Documents and Settings\eamonn.keogh\Desktop\StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempStringDim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempStringDim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)Dim strTempString As String strTempString = myStreamReader.ReadLine() bntRedDemo.Text = strTempStringA little “trick”, if you don’t give the path, VB assumes that the file is in the same directory as the program itself.Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read)Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read)This line of code opens the file, read for some action.Why must we explicitly open (and close) files?The path and name of the file to openAppendOpens the file if it exists and seeks to the end of the file, or creates a new file. Only works in conjunction with FileAccess.Write.CreateSpecifies that the operating system should create a new file. Overwrites the file if it exists.CreateNewSpecifies that the operating system should create a new file. If the file exists, an error occurs.OpenSpecifies that the operating system should open an existing file.OpenOrCreateSpecifies that the operating system should open a file if it exists; otherwise, a new file should be created.TruncateSpecifies that the operating system should open an existing file and clear its contents.The file modeReadData can be read from the file. Combine with Write for read/write access.ReadWriteData can be written to and read from the file.WriteData can be written to the file.The file actionDim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)Dim strTempString As StringstrTempString = myStreamReader.ReadLine()bntRedDemo.Text = strTempStringDim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)Dim strTempString As StringstrTempString = myStreamReader.ReadLine()bntRedDemo.Text = strTempStringIf I wanted to write data to the stream, I would use…Dim myStreamReader As New StreamWriter(myStream)Dim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000)End WhileDim myStream As New FileStream("StudentData.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000)End While…..Peek equals –1 when the end of the file is reachedDim myStream As New FileStream("StudentXX.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000)End WhileDim myStream As New FileStream("StudentXX.txt", FileMode.Open, FileAccess.Read) Dim myStreamReader As New StreamReader(myStream)While myStreamReader.Peek() <> -1 bntRedDemo.Text = myStreamReader.ReadLine() bntRedDemo.Refresh() Sleep(1000)End WhileFile StudentXX.txt does not exist!Trying to open a file that does not exist causes an error..Note I am using “FileMode.Open”, if I were using one of the other options…If Not (File.Exists("StudentXX.txt")) Then bntRedDemo.Text = "The file does not exist!" ElseDim myStream As New FileStream("StudentXX.txt",
View Full Document