Tutorial 9 Sequential Access Files and Printing Tutorial 9: Sequential Access Files and Printing 1 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 Tutorial 9: Sequential Access Files and Printing 2 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 Tutorial 9: Sequential Access Files and Printing 3 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) Tutorial 9: Sequential Access Files and Printing 4 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 Tutorial 9: Sequential Access Files and Printing 5 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”) Tutorial 9: Sequential Access Files and Printing 6 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 Tutorial 9: Sequential Access Files and Printing 7 Writing to a Sequential File Write method Leaves the file pointer after last character objStreamWriter.Write(“Good”) objStreamWriter.Write(“ Morning”) ‘ Writes on the same line Good Morning WriteLine method Appends 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 Tutorial 9: Sequential Access Files and Printing 8 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) Tutorial 9: Sequential Access Files and Printing 9 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 Tutorial 9: Sequential Access Files and Printing 10 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() Tutorial 9: Sequential Access Files and Printing 11 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 Tutorial 9: Sequential Access Files and Printing 12 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 Tutorial 9: Sequential Access Files and Printing 13 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 Tutorial 9: Sequential Access Files and Printing 14 Tutorial 9: Sequential Access Files and Printing 15 The Value Property Example Results Me.PayDateTimePicker.Value 11/12/2004 9:07:00 AM Me.PayDateTimePicker.Value.ToLongDateString Friday, November 12, 2004 Me.PayDateTimePicker.Value.ToShortDateString 11/12/2004 Me.PayDateTimePicker.Value.ToLongTimeString 9:08:00 AM Me.PayDateTimePicker.Value.ToShortTimeString 9:08 AM Tutorial 9: Sequential Access Files and Printing 16 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) Tutorial 9: Sequential Access Files and Printing 17 Retrieving the Information Stored in the Value Property Example using “11/12/2004 9:07 AM” Results Me.PayDateTimePicker.Value.Month 11 Me.PayDateTimePicker.Value.Day 12 Me.PayDateTimePicker.Value.Year 2004 Me.PayDateTimePicker.Value.DayOfWeek 5 Me.PayDateTimePicker.Value.DayOfWeek.ToString Friday Me.PayDateTimePicker.Value.Hour 9 Me.PayDateTimePicker.Value.Minute 7 Me.PayDateTimePicker.Value.Second 0 Tutorial 9: Sequential Access Files and Printing 18 Tutorial 9: Sequential Access Files and Printing 19