DOC PREVIEW
UCR CS 5 - Chapter 9 Files

This preview shows page 1-2-3-4-26-27-28-54-55-56-57 out of 57 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 57 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 9 – FilesSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57The Visual Basic .NET Coach1Chapter 9 – FilesA simple way to store data so that it is accessible the next time you wish to execute the application is to store it in a data file. There are three types of data files: binary, sequential and random access.The Visual Basic .NET Coach29.1 Sequential FilesA sequential file allows reading or writing the file from the beginning of the file until the end of the file. Files that are read and written sequentially are plain text files commonly known as ASCII files. ASCII stands for American Standard Code for Information Interchange. You can view or edit small ASCII files using the Notepad application that comes with Windows, or you can use a word processor and then select the ASCII option when you save the file.Chapter 9 – FilesThe Visual Basic .NET Coach3If you wanted to save the information in your Student Search application a sequential file of this information might look like one of the following two files:Chapter 9 – Files1 Salvage Jeff Computer Science Senior 4.02 Cunningham John Basket Weaving Freshman 1.03 Pepito Suzanne Industrial Engineering Junior 3.84 Burke Tim MIS Senior 3.925 Fletcher Irwin Film Senior 3.991, "Salvage", "Jeff", "Computer Science", "Senior", 4.02, "Cunningham", "John", "Basket Weaving", "Freshman", 1.03, "Pepito", "Suzanne", "Industrial Engineering", "Junior", 3.84, "Burke" ,"Tim", "MIS", "Senior", 3.925, "Fletcher", "Irwin", "Film", "Senior", 3.99The Visual Basic .NET Coach4A fixed-width file contains information formatted so that each line stores each data item in a fixed location within the line. The comma-delimited file stores each data item with a comma separating each item. A comma-delimited file will also place double quotes around String fields.One uses sequential file access when the application reads all the data at once or writes all the data at once. Either a fixed-width or a comma-delimited file can be used.A comma-delimited file can save space if many of the strings’ average sizes are significantly smaller than their maximums. A fixed-width file sets size limits on the fields that are not required with comma-delimited files.Chapter 9 – FilesThe Visual Basic .NET Coach59.2 Fixed-Width FilesVisual Basic .NET provides an object interface for accessing fixed-width formatted files. You can access a file for both input and output by using a combination of three classes—FileStream, StreamWriter, and StreamReader. In order to access these classes, you must include the namespace System.IO with an Imports statement at the beginning of your file.Chapter 9 – FilesThe Visual Basic .NET Coach6General Access of Fixed-Width FilesStep 1: Add the Imports statement to the project at the top of your form.Chapter 9 – FilesImports System.IOThe Visual Basic .NET Coach7General Access of Fixed-Width Files ContinuedStep 2: When reading fixed-width files it is easiest to read data from a file one line at a time and then divide the data read into individual values. To accomplish this you first must open the text file using the FileStream object. Chapter 9 – FilesDim FileStreamName As New FileStream("Path and File Name", _ FileMode.Open, FileAccess.Read)Dim FileStreamName As New FileStream("Path and File Name", _ FileMode.OpenOrCreate, FileAccess.Write)FileStreamName is the name of the object that you will use to reference the file from within the application.The first parameter is the path to the file and the actual file name of the file that you are opening. The second parameter specifies the action upon opening the file: Append, Create, CreateNew, Open, OpenOrCreate, or Truncate. Use the Open keyword when opening a file for input and the OpenOrCreate keyword to write to a file.The final parameter indicates the action(s) that will be performed upon the file once it is open: Read, ReadWrite, or Write. Use the Read keyword for input and the Write keyword to write to a file. When you specify ReadWrite, the file can be either read from or written to.The Visual Basic .NET Coach8General Access of Fixed-Width Files ContinuedStep 3: While the FileStream object will open the file, you need another object to perform an action on the file. The two typical actions to perform are reading and writing to the file. Visual Basic .NET includes two objects, the StreamReader and the StreamWriter.A StreamReader object allows to read data easily from the file just opened. Instantiating a StreamReader object requires passing it a FileStream object:Chapter 9 – FilesDim StreamReaderName As New StreamReader(FileStreamName)A StreamWriter object allows to write data from the file just opened:Dim StreamWriterName As New StreamWriter(FileStreamName)The Visual Basic .NET Coach9General Access of Fixed-Width Files ContinuedStep 4: Once you have a StreamReader or StreamWriter object instantiated, you can read or write a line of data using the following syntax:Chapter 9 – FilesstrStringVariable = srStreamReaderVariable.ReadLine()orsrStreamWriterVariable.WriteLine(strStringVariable)In the case of the StreamReader, a line of input is read from the file and stored in a String variable. In the case of the StreamWriter, a String variable is passed to the object and written to the file.The Visual Basic .NET Coach10General Access of Fixed-Width Files ContinuedStep 5a: In order to read data from a fixed-width file, you must divide the single String read using the ReadLine method of the StreamReader object. This can be accomplished using the Substring method of the String class.Chapter 9 – FilesThe Visual Basic .NET Coach11General Access of Fixed-Width Files ContinuedStep 5b: In order to write data to a fixed-width file, you must format the data so that the appropriate number of spaces follow each data element.The following is what two strings, "Jeff" and "Salvage", each padded to 10 characters, would look like if they were concatenated together:Chapter 9 – Files"Jeff Salvage


View Full Document

UCR CS 5 - Chapter 9 Files

Download Chapter 9 Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 9 Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 9 Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?