Download presentation
Presentation is loading. Please wait.
Published byGwendolyn Hensley Modified over 9 years ago
1
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing
2
Tutorial 9: Sequential Access Files and Printing2 Sequential Access Files Lesson A Objectives After completing this lesson, you will be able to: Declare StreamReader and StreamWriter object variables Open a sequential access file Determine whether a sequential file exists Write information to a sequential access file Align the text written to a sequential access file Read information from a sequential access file Determine whether the computer has finished reading a sequential access file Close a sequential access file
3
Tutorial 9: Sequential Access Files and Printing3 File Types Files to which information is written are called output files, because the files store the output produced by an application Files that are read by the computer are called input files, because an application uses the information in these files as input Here is a list of different file types: Sequential access files Random access files Binary access files
4
Tutorial 9: Sequential Access Files and Printing4 Using 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)
5
Tutorial 9: Sequential Access Files and Printing5 Procedure for Using a Sequential Access File 1.Declare either a StreamWriter or StreamReader object variable 2.Create a StreamWriter or StreamReader object by opening a file; assign the object’s address to the object variable declared in Step 1 3.Use the StreamWriter object to write one or more lines of text to the file, or use the StreamReader object to read one or more lines of text from the file 4.Use the StreamWriter or StreamReader object to close the file
6
Tutorial 9: Sequential Access Files and Printing6 Opening a Sequential File Refer to Figure 9-7 in the textbook, which illustrates all of the methods used to open a sequential access file CreateText method – Creates a file for writing a text stream objStreamWriter = System.IO.File.CreateText(“memo.txt”) AppendText method – Opens a file appending text to the end of the file objStreamWriter = System.IO.File.AppendText(“sales.txt”) OpenText method – Opens a file to read a stream of text objStreamReader = System.IO.File.OpenText(“a:\reports\pay.txt”)
7
Tutorial 9: Sequential Access Files and Printing7 Does the file exist? Avoid errors by using the Exists method If System.IO.File.Exists(“pay.txt”) Then Returns True or False The Exists method returns the Boolean value True if filename exists; otherwise, it returns the Boolean value False
8
Tutorial 9: Sequential Access Files and Printing8 Writing to a Sequential File Write methodLeaves the file pointer after last character objStreamWriter.Write(“Good”) objStreamWriter.Write(“ Morning”) ‘ Writes on the same line Good Morning WriteLine methodAppends the line terminator to the end of line and leaves the file pointer ready for next line objStreamWriter.WriteLine(“Good”) objStreamWriter.WriteLine(“ Morning”) ’ Each is on a separate line Good Morning
9
Tutorial 9: Sequential Access Files and Printing9 Formatting String Data The Space function inserts spaces objStreamWriter.Write(Space(10) & “A” & Space(5) & “B” PadLeft – Pads a string on the left with specified character Dim sngNetPay As Single = 767.89, strNet As String strNet = FormatCurrency (sngNetPay) strNet = strNet.PadLeft(15, “*”) Results in “********$767.89” PadRight – Pads a string on the right with specified character Dim strName As String = “Sue” strName = strName.PadRight(10) Results in “Sue ” (the string “Sue” and seven spaces)
10
Tutorial 9: Sequential Access Files and Printing10 Aligning Data in a Sequential File For intRegion = 1 To 3 strSales = InputBox(“Sales amount”, “Sales”) strSales = Format(strSales, “standard”) objStreamWriter.WriteLine(strSales.PadLeft(8)) Next intRegion Result (assuming the following sales amounts: 645.75, 1200, 40.80 645.75 1,200.00 40.80
11
Tutorial 9: Sequential Access Files and Printing11 Reading from a Sequential File ReadLine Method – Read a line of text from the file strLine = objStreamReader.ReadLine() Peek method – Looks to see if there is another character to read Do Until objStreamReader.Peek = -1 Close method – Close the file when you are finished objStreamReader.Close() objStreamWriter.Close()
12
Tutorial 9: Sequential Access Files and Printing12 Using a DateTimePicker Control Lesson B Objectives After completing this lesson, you will be able to: Add a DateTimePicker control to a form Control the appearance of a DateTimePicker control Format the text that appears in a DateTimePicker control Set and retrieve the information stored in a DateTimePicker control Retrieve the system date and time Display a form immediately
13
Tutorial 9: Sequential Access Files and Printing13 Adding a DateTimePicker Control to a Form The DateTimePicker control allows the user to select either a date or time, and then displays the selected information in a specified format
14
Tutorial 9: Sequential Access Files and Printing14 DateTimePicker Properties The DateTimePicker control’s ShowUpDown property determines whether a list arrow button (default) or up and down arrow button appear on the control You can use the Format property to control the format (style) of the date or time displayed in the DateTimePicker control When you add a DateTimePicker control to a form, Visual Basic.NET retrieves the current date and time from your computer system’s clock, and assigns both values to the DateTimePicker control’s Value property The text that appears in a DateTimePicker control is stored in the control’s Text property
15
Tutorial 9: Sequential Access Files and Printing15
16
Tutorial 9: Sequential Access Files and Printing16 The Value Property ExampleResults Me.PayDateTimePicker.Value11/12/2004 9:07:00 AM Me.PayDateTimePicker.Value.ToLongDateStringFriday, November 12, 2004 Me.PayDateTimePicker.Value.ToShortDateString11/12/2004 Me.PayDateTimePicker.Value.ToLongTimeString9:08:00 AM Me.PayDateTimePicker.Value.ToShortTimeString9:08 AM
17
Tutorial 9: Sequential Access Files and Printing17 Using the DateTime Object objectname.Value = New DateTime(year, month, day[, hour, minute, second]) Me.PayDateTimePicker.Value = New DateTime(2004, 2, 3) Me.PayDateTimePicker.Value = New DateTime(2004, 9, 7, 5, 30, 18) Me.PayDateTimePicker.Value = New DateTime(2004, 1, 6, 17, 30, 18)
18
Tutorial 9: Sequential Access Files and Printing18 Retrieving the Information Stored in the Value Property Example using “11/12/2004 9:07 AM”Results Me.PayDateTimePicker.Value.Month11 Me.PayDateTimePicker.Value.Day12 Me.PayDateTimePicker.Value.Year2004 Me.PayDateTimePicker.Value.DayOfWeek5 Me.PayDateTimePicker.Value.DayOfWeek.ToStringFriday Me.PayDateTimePicker.Value.Hour9 Me.PayDateTimePicker.Value.Minute7 Me.PayDateTimePicker.Value.Second0
19
Tutorial 9: Sequential Access Files and Printing19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.