Title: Chapter 9: Sequential Access Files and Printing
1Chapter 9 Sequential Access Files and Printing
Programming with Microsoft Visual Basic .NET,
Second Edition
2Sequential Access FilesLesson A Objectives
- Declare StreamReader and StreamWriter 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
3Sequential Access FilesLesson A Objectives
(continued)
- Read information from a sequential access file
- Determine whether the computer has finished
reading a sequential access file - Close a sequential access file
4File Types
- Files to which information is written are called
output files - The files store the output produced by an
application - Files that are read by the computer are called
input files - An application uses the information in these
files as input
5File Types (continued)
- Here is a list of different file types
- Sequential access files
- Random access files
- Binary access files
6Using Sequential Access Files
- A sequential access file is often referred to as
a text file, because it is composed of lines of
text - Sequential access files are similar to cassette
tapes in that each line in the file, like each
song on a cassette tape, is both stored and
retrieved in consecutive order (sequentially)
7Using Sequential Access Files (continued)
Figure 9-5 Procedure for using a sequential
access file
8Declaring StreamWriter and StreamReader Variables
- Use a StreamWriter object to write a sequence of
characters to a sequential access file - The sequence of characters is referred to as a
stream of characters (or a stream) - Use a StreamReader object to read a stream
(sequence of characters) from a sequential access
file
9Declaring StreamWriter and StreamReader Variables
(continued)
- Before creating the appropriate object, first
declare an object variable to store the address
of the object in the computers internal memory - Use a StreamWriter variable to store the address
of a StreamWriter object - Use a StreamReader variable to store the address
of a StreamReader object
10Declaring StreamWriter and StreamReader Variables
(continued)
Figure 9-6 Syntax and examples of declaring
StreamWriter and StreamReader variables
11Opening a Sequential Access File
- The OpenText method opens an existing sequential
access file for input and allows the computer to
read the information stored in the file - Use the CreateText method to create a new, empty
sequential access file to which data can be
written - Use the AppendText method to add data to the end
of an existing sequential access file
12Opening a Sequential Access File (continued)
Figure 9-7 Syntax and examples of opening a
sequential access file
13Opening a Sequential Access File (continued)
Figure 9-7 Syntax and examples of opening a
sequential access file (continued)
14Opening a Sequential Access File (continued)
Figure 9-8 Position of the file pointer when
files are opened for input, output, and append
15Determining Whether a File Exists
- Avoid errors by using the Exists method
- Syntax IO.File.Exists(filename)
- The Exists method returns the Boolean value True
if filename exists otherwise, it returns the
Boolean value False
16Writing Information to a Sequential Access File
- Use either the Write method or the WriteLine
method to write information to a sequential
access file - Write method
- Positions the file pointer at the end of the last
character it writes to the file - Syntax variablename.Write(data)
17Writing Information to a Sequential Access File
(continued)
- WriteLine method
- Positions the file pointer at the beginning of
the next line in the file - Syntax variablename.WriteLine(data)
18Reading Information from a Sequential Access File
- ReadLine method
- Read a line of text from the file
- Syntax variablename.ReadLine()
- Peek method
- Looks to see if there is another character to
read - Syntax variablename.Peek()
19Closing a Sequential Access File
- Close method closes the file when you are
finished - Syntax variablename.Close()
- Variablename is the name of either a StreamReader
or StreamWriter variable
20The Friends Application
- Allows the user to
- Write the names of his or her friends to a
sequential access file - Read the names from the file
21The Friends Application (continued)
Figure 9-14 User interface for the Friends
application
22The Friends Application (continued)
Figure 9-15 Pseudocode for the Write to File and
Read from File buttons
23Records In a Sequential Access File Lesson B
Objective
- Write records to a sequential access file
- Read records from a sequential access file
24Writing and Reading Records
- A sequential access file can be used to store
fields and records - A field is a single item of information about a
person, place, or thing - A record is one or more related fields that
contain all of the necessary data about a
specific person, place, or thing
25Writing and Reading Records (continued)
- When writing records to a sequential access file,
you typically write each record on a separate
line in the file - If the records contain more than one field, you
can separate each field with a special character,
such as a comma or the number symbol ()
26Writing and Reading Records (continued)
Figure 9-21 Examples of writing a record to a
sequential access file
27Writing and Reading Records (continued)
Figure 9-22 Examples of reading records from a
sequential access file
28Creating the PAO Application
- Application for the PAO (Political Awareness
Organization) should allow the organizations
secretary to - Save (to a sequential access file) a voters
political party and age - Tabulate the number of Democrats, Republicans,
and Independents entered in the file - Print the contents of the file
29Creating the PAO Application (continued)
Figure 9-23 Interface for the PAO application
30Coding the PaoForm Load Event Procedure
Figure 9-25 Pseudocode for the PaoForms Load
event procedure
31Coding the uiWriteButton Click Event Procedure
Figure 9-30 Pseudocode for the uiWriteButtons
Click event procedure
32Coding the uiDisplayButton Click Event Procedure
Figure 9-34 Pseudocode for the uiDisplayButtons
Click event procedure
33Coding the uiDisplayButton Click Event Procedure
(continued)
Figure 9-34 Pseudocode for the uiDisplayButtons
Click event procedure (continued)
34Printing a Sequential Access FileLesson C
Objective
- Add a PrintDocument control to a form
- Print text using the Print and e.Graphics.DrawStri
ng methods - Code a PrintDocument controls PrintPage event
procedure
35Adding a PrintDocument Control to the Form
- To complete the PAO application, you need to
- Add a PrintDocument control to the form
- Code the Print Report buttons Click event
procedure and the PrintDocument controls
PrintPage event procedure
36Coding the Print Report Button Click Event
Procedure
- The Print Report buttons Click event procedure
is responsible for using the uiReportPrintDocument
control to print the contents of the pao.txt
sequential access file - The Print method causes the PrintDocument
controls PrintPage event to occur - You use the PrintPage event to indicate the
information you want to print, as well as how you
want the information to appear in the printout
37Coding the Print Report Button Click Event
Procedure (continued)
Figure 9-39 Pseudocode for the Print Report
buttons Click event procedure
38Coding the PrintPage Event Procedure
- The PrintPage event procedure should print the
contents of the pao.txt file in a report format - The report should contain a report header and two
columns of information - The first column should list the party
- The second column should list the age
39Coding the PrintPage Event Procedure (continued)
Figure 9-41 Pseudocode for the
uiReportPrintDocuments PrintPage event procedure
40The e.Graphics.DrawString Method
- You use the e.Graphics.DrawString method to print
text on the printer - Some print fonts are proportionally spaced, while
others are fixed-spaced, often referred to as
mono-spaced
41The e.Graphics.DrawString Method (continued)
- Fixed-spaced fonts use the same amount of space
to print each character - Proportionally spaced fonts use varying amounts
of space to print characters
42Summary
- To declare a StreamWriter or StreamReader
variable, use the syntaxDim Private
variablename As IO.objecttype - To open a sequential access file for input, use
the syntax IO.File.OpenText(filename) - To open a sequential access file for append, use
the syntax IO.File.AppendText(filename) - To open a sequential access file for output, use
the syntax IO.File.CreateText(filename)
43Summary (continued)
- To write a record to a sequential access file,
you typically write each record on a separate
line in the file - If the records contain more than one field,
separate each field with a special character - To read a record from a sequential access file,
use the ReadLine method to read a line of text
from the file the line of text is the record
44Summary (continued)
- To print text within a Visual Basic .NET
application - Include a PrintDocument control in the
application - Use the Print method to print the document
- Use the PrintPage event procedure to indicate the
information you want to print and how you want
the information to appear in the printout - Use the syntax e.Graphics.DrawString(string,
font, Brushes.Black, horizontalPosition,
verticalPosition) to print the document