Chapter Microsoft Visual Basic NET Reloaded Sequential Access
Chapter Microsoft Visual Basic. NET: Reloaded Sequential Access Files and Error Handling
Objectives • Declare Stream. Reader and Stream. Writer variables • Open a sequential access file • Determine whether a sequential access file exists • Write information to a sequential access file • Align the text written to a sequential access file Microsoft Visual Basic. NET: Reloaded 2
Objectives (continued) • Read information from a sequential access file • Test for the end of a sequential access file • Close a sequential access file • Handle exceptions using a Try/Catch block • Write records to a sequential access file • Read records from a sequential access file Microsoft Visual Basic. NET: Reloaded 3
File Types • Output files • Files to which output is written • Store output produced by the application • Input Files • Files that are read by the computer • Application uses information stored in files • Sequential • Often referred to as “text files” • Binary and Random • Not covered in this text Microsoft Visual Basic. NET: Reloaded 4
Using Sequential Access Files Microsoft Visual Basic. NET: Reloaded 5
HOW TO… Microsoft Visual Basic. NET: Reloaded 6
Declaring Stream. Writer and Stream. Reader Variables • Stream. Writer object • Writes a sequence of characters to a sequential access file • Referred to as a “stream of characters” • Also referred to as a “stream” • Stream. Reader object • Reads a stream (sequence of characters) to a sequential access file Microsoft Visual Basic. NET: Reloaded 7
HOW TO… Microsoft Visual Basic. NET: Reloaded 8
Opening a Sequential Access file • Declare variable of Stream. Writer or Stream. Reader object datatype • Then use variable to refer to object and file in the program • Open. Text method • Opens an existing file for input • Create. Text method • Creates a new empty file for output • Append. Text method • Appends data to the end of an existing sequential access file Microsoft Visual Basic. NET: Reloaded 9
HOW TO… Microsoft Visual Basic. NET: Reloaded 10
Opening a Sequential Access file (continued) Microsoft Visual Basic. NET: Reloaded 11
Determining Whether a File Exists Microsoft Visual Basic. NET: Reloaded 12
Writing Information to a Sequential Access File • Write method • Positions file pointer at end of last character it writes to the file • Write. Line method • Positions file pointer at beginning of next line in the file by appending a line termination character (carriage return, line feed) • Space function • Use to write a specific number of spaces to file Microsoft Visual Basic. NET: Reloaded 13
HOW TO… Microsoft Visual Basic. NET: Reloaded 14
Aligning Columns of Information • Pad. Left(length, [character]) • Pads at beginning of string with a specified character until string is a specified length • Character is optional and default is a space • Pad. Right(length, [character]) • Same as above only pads at end of string Microsoft Visual Basic. NET: Reloaded 15
HOW TO… Microsoft Visual Basic. NET: Reloaded 16
Reading Information from a Sequential Access file • Read. Line method • Reads a line of text from sequential access file • A line is a sequence of characters followed by the line termination character • Peek method • Peeks into a file to see if file contains another character to read • Returns the number -1 if no more data in file • Typically used within a loop to prevent a file read error Microsoft Visual Basic. NET: Reloaded 17
HOW TO… Microsoft Visual Basic. NET: Reloaded 18
Closing a Sequential Access File • Close method • Closes file associate with stream variable Microsoft Visual Basic. NET: Reloaded 19
The Friends Application • Allows user to write names of his or her friends to a sequential access file • Allows names to be read from above file Microsoft Visual Basic. NET: Reloaded 20
The Friends Application (continued) Microsoft Visual Basic. NET: Reloaded 21
The Friends Application (continued) Microsoft Visual Basic. NET: Reloaded 22
The Friends Application (continued) Microsoft Visual Basic. NET: Reloaded 23
HOW TO… Microsoft Visual Basic. NET: Reloaded 24
The Friends Application (continued) • btn. Read_Click event • Code causes name read from file to be displayed in a message box Microsoft Visual Basic. NET: Reloaded 25
Using a Try/Catch Block • Try statement • Use to catch (or trap) an exception • Exception is an error that occurs while program is running • Catch statement • Use to have computer take appropriate action • More than one catch can occur after a try • Multiple catch allow for trapping of different types of errors • Try/Catch block • Block of code that uses both Try and Catch statements Microsoft Visual Basic. NET: Reloaded 26
HOW TO… Microsoft Visual Basic. NET: Reloaded 27
HOW TO…(continued) Microsoft Visual Basic. NET: Reloaded 28
Using a Try/Catch Block (continued) Microsoft Visual Basic. NET: Reloaded 29
Using a Try/Catch Block (continued) Microsoft Visual Basic. NET: Reloaded 30
Writing and Reading Records • Field • A single item of information about a person, place, or thing • Examples: Name, salary, price, age • Record • One or more related fields that contain all the necessary data about a specific person, place, or thing Microsoft Visual Basic. NET: Reloaded 31
HOW TO… Microsoft Visual Basic. NET: Reloaded 32
HOW TO… Microsoft Visual Basic. NET: Reloaded 33
Programming Example - PAO Application • Application allows user to enter the political party and age • The input information is saved to a sequential access file • In addition, the application calculates and displays the number of voters in each political party Microsoft Visual Basic. NET: Reloaded 34
TOE Chart Microsoft Visual Basic. NET: Reloaded 35
TOE Chart (continued) Microsoft Visual Basic. NET: Reloaded 36
User Interface Microsoft Visual Basic. NET: Reloaded 37
Objects, Properties, and Settings Microsoft Visual Basic. NET: Reloaded 38
Objects, Properties, and Settings (continued) Microsoft Visual Basic. NET: Reloaded 39
Tab Order Microsoft Visual Basic. NET: Reloaded 40
Pseudocode btn. Exit Click event procedure close the application btn. Write Click event procedure open pao. txt file for append if txtparty control contains D, R, or I Write contents of txt. Party control, a comma, and txt. Age to pao. txt file else Display message prompting user to enter D, R, or I End if close the pao. txt file Clear contents of txt. Party and txt. Age controls Send focus to txt. Party control Include a Try/Catch block to catch general errors displaying error description in a messagebox Microsoft Visual Basic. NET: Reloaded 41
Pseudocode (continued) btn. Display click event procedure open pao. txt file for input repeat until no more characters to read record from file if first character is letter D add 1 to Democrat counter else if first character is R add 1 to Republican counter else add 1 to Independent counter end if end try close pao. txt file Microsoft Visual Basic. NET: Reloaded 42
Pseudocode (continued) btn. Display Click event (continued) Display Democrat counter value in lbl. Total. Dem Display Republican counter value in lbl. Total. Rep Display Independent counter value in lbl. Total. Ind Include Try/Catch block with 2 Catch sections The first catches File. Not. Found. Exception and displays “Cannot locate the pao. txt file” in a messagebox The second catches general errors and displays a description of the error in a messagebox Microsoft Visual Basic. NET: Reloaded 43
Code Microsoft Visual Basic. NET: Reloaded 44
Code (continued) Microsoft Visual Basic. NET: Reloaded 45
Summary • Information in a sequential access file is accessed in consecutive order (from beginning to end) • Use the Stream. Writer object to write a sequence of characters (stream) to a sequential access file • Use a Stream. Reader object to read a stream from a sequential access file • Exists method returns a boolean value indicating whether file exists Microsoft Visual Basic. NET: Reloaded 46
Summary (continued) • Write method writes but does not write a line termination characters • Write. Line method appends a line termination character • Read. Line method reads a line of text • Peek method determines whether file has more data to be read • Space function writes specific number of spaces to file • Pad. Left and Pad. Right pad characters to file to align columns of information Microsoft Visual Basic. NET: Reloaded 47
Summary (continued) • To prevent loss of data, call the Close method • Use a Try/Catch block to catch an exception (error) and to take appropriate action when exception occurs • Use a general Catch statement as the last Catch statement to handle unexpected errors • Display exception using syntax variablename. Message where variablename is the name of variable in catch statement • Sequential files are used to store fields and records Microsoft Visual Basic. NET: Reloaded 48
- Slides: 48