Clearly Visual Basic Programming with Visual Basic 2008
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 23 I’m Saving for the Future
Objectives • Open and close a sequential access file • Write information to and read information from a sequential access file • Determine whether a sequential access file exists • Test for the end of a sequential access file Clearly Visual Basic: Programming with Visual Basic 2008 2
Sequential Access Files • Output files – Files to which data is written • Input files – Files read by the computer • Sequential access files – Input and output files composed of lines of text that are both read and written sequentially Clearly Visual Basic: Programming with Visual Basic 2008 3
Clearly Visual Basic: Programming with Visual Basic 2008 4
Write Those Lines of Text • Stream of characters – A sequence of characters • Stream. Writer object – Used to write a stream of characters to a sequential access file • Before creating the Stream. Writer object: – First declare a variable to store the object in the computer’s internal memory Clearly Visual Basic: Programming with Visual Basic 2008 5
Clearly Visual Basic: Programming with Visual Basic 2008 6
Write Those Lines of Text (continued) • Create. Text method – Used to open a sequential access file for output • Append. Text method – Used to open a sequential access file for append • Write method or the Write. Line method – Used to write data to a file • Write. Line method – Writes a newline character after the data Clearly Visual Basic: Programming with Visual Basic 2008 7
Clearly Visual Basic: Programming with Visual Basic 2008 8
Clearly Visual Basic: Programming with Visual Basic 2008 9
Now Read Those Lines of Text • Stream. Reader object – Used to read data from a sequential access file – Before creating: • First declare a variable to store the object in the computer’s internal memory • Open. Text method – Used to open a sequential access file for input, which automatically creates a Stream. Reader object • Exists method – Returns the Boolean value True if the file exists • Otherwise, returns the Boolean value False Clearly Visual Basic: Programming with Visual Basic 2008 10
Clearly Visual Basic: Programming with Visual Basic 2008 11
Clearly Visual Basic: Programming with Visual Basic 2008 12
Clearly Visual Basic: Programming with Visual Basic 2008 13
Now Read Those Lines of Text (continued) • Read. Line method – Used to read the file’s contents, one line at a time • Line – Defined as a sequence (stream) of characters followed by the newline character • Peek method – “Peeks” into the file to determine whether the file contains another character to read Clearly Visual Basic: Programming with Visual Basic 2008 14
Clearly Visual Basic: Programming with Visual Basic 2008 15
Summary • An application – Can both write data to and read data from a sequential access file • Stream. Writer object – Used to write a sequence (stream) of characters to a sequential access file • To write data to a sequential access file: – Use either the Write or Write. Line method • Use the Close method to: – Close a sequential access file as soon as you are finished using the file Clearly Visual Basic: Programming with Visual Basic 2008 16
Summary (continued) • Use a text box’s Focus method to – Send the focus to the text box • Exists method – Returns a Boolean value that indicates whether a sequential access file exists • If a file contains another character to read: – The Peek method returns the character; otherwise, it returns the number -1 Clearly Visual Basic: Programming with Visual Basic 2008 17
- Slides: 17