Clearly Visual Basic Programming with Visual Basic 2010
Clearly Visual Basic: Programming with Visual Basic 2010 2 nd Edition Chapter 22 I’m Saving for the Future (Sequential Access Files)
Objectives After studying Chapter 22, you should be able to: Open and close a sequential access file Write data to a sequential access file Send the focus to a text box Read data from a sequential access file Determine whether a sequential access file exists Test for the end of a sequential access file 2 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Sequential Access Files Output files Store output produced by an application 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 Also called text files 3 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Sequential Access Files (cont’d. ) Figure 22 -1 Interface for the Game Show Contestants application 4 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
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 5 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -2 Syntax and an example of declaring a Stream. Writer variable 6 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Write Those Lines of Text (cont’d. ) 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 7 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -3 Syntax and examples of creating a Stream. Writer object by opening a file 8 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -4 Syntax and examples of writing data to a sequential access file 9 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Write Those Lines of Text (cont’d. ) Focus method Sends the focus to a text box Syntax: textbox. Focus() Close method Closes a sequential access file Close the file as soon as you are finished using it Syntax: stream. Writer. Variable. Name. Close() 10 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
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 Automatically creates a Stream. Reader object 11 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Now Read Those Lines of Text (cont’d. ) Exists method Returns the Boolean value True if the file exists Otherwise, returns the Boolean value False 12 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -7 Syntax and an example of declaring a Stream. Reader variable 13 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -8 Syntax and an example of creating a Stream. Reader object by opening a file 14 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -9 Syntax and an example of the Exists method 15 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Now Read Those Lines of Text (cont’d. ) 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 16 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -11 Syntax and an example of the Read. Line method 17 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Figure 22 -12 Syntax and an example of the Peek method 18 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
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 19 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
Summary (cont’d. ) 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 20 Clearly Visual Basic: Programming with Visual Basic 2010, 2 nd Edition
- Slides: 20